Elasticsearch 6.5.4 Java Api
时间: 2023-12-20 09:57:26 浏览: 116
以下是 Elasticsearch 6.5.4 Java API 的一些示例:
1. 连接 Elasticsearch:
```
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
```
2. 索引文档:
```
IndexRequest request = new IndexRequest("index_name", "type_name", "document_id");
String jsonString = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
request.source(jsonString, XContentType.JSON);
IndexResponse response = client.index(request);
```
3. 搜索文档:
```
SearchRequest searchRequest = new SearchRequest("index_name");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("field_name", "search_text"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
```
4. 删除文档:
```
DeleteRequest request = new DeleteRequest("index_name", "type_name", "document_id");
DeleteResponse response = client.delete(request);
```
注意:以上示例代码需要使用 Elasticsearch 6.5.4 版本的 Java API 和对应的 Elasticsearch 服务器版本。
阅读全文