RestHighLevelClient在ES7.17中已经被淘汰,请使用高级客户端ElasticsearchClient
时间: 2024-05-28 08:12:06 浏览: 43
是的,您说得对。在 Elasticsearch 7.17 中,RestHighLevelClient 已经被宣布为过时,并且应该使用 ElasticsearchClient 代替。ElasticsearchClient 是 Elasticsearch 官方提供的新的 Java 客户端,它提供了更好的性能和更好的可扩展性,并且可以与 Elasticsearch 7.0 及更高版本兼容。它提供了许多新的功能和改进,例如连接池、异步请求和响应处理、请求跟踪、请求失败处理等等。如果您正在使用 Elasticsearch 7.17 及更高版本,建议您使用 ElasticsearchClient 来与 Elasticsearch 进行交互。
相关问题
在 Elasticsearch 7.17 中,RestHighLevelClient 已经被宣布为过时,请使用ElasticsearchClient,写个操作ES的工具类
好的,下面是一个操作 Elasticsearch 的工具类:
```java
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.Map;
public class EsUtil {
private static RestHighLevelClient client;
static {
RestClientBuilder builder = RestClient.builder(
new HttpHost("localhost", 9200, "http")
);
client = new RestHighLevelClient(builder);
}
/**
* 创建索引
*
* @param indexName 索引名称
* @param settings 索引配置
* @param mappings 索引映射
* @return 是否创建成功
*/
public static boolean createIndex(String indexName, Settings settings, String mappings) {
try {
CreateIndexRequest request = new CreateIndexRequest(indexName);
request.settings(settings);
request.mapping(mappings, XContentType.JSON);
CreateIndexResponse response = client.indices().create(request);
return response.isAcknowledged();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 判断索引是否存在
*
* @param indexName 索引名称
* @return 是否存在
*/
public static boolean existsIndex(String indexName) {
try {
return client.indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 删除索引
*
* @param indexName 索引名称
* @return 是否删除成功
*/
public static boolean deleteIndex(String indexName) {
try {
AcknowledgedResponse response = client.indices().delete(new DeleteIndexRequest(indexName), RequestOptions.DEFAULT);
return response.isAcknowledged();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 新增文档
*
* @param indexName 索引名称
* @param id 文档id
* @param source 文档内容
* @return 是否新增成功
*/
public static boolean indexDocument(String indexName, String id, Map<String, Object> source) {
try {
IndexRequest request = new IndexRequest(indexName);
request.id(id);
request.source(source);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
return response.getResult() == DocWriteResponse.Result.CREATED || response.getResult() == DocWriteResponse.Result.UPDATED;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 删除文档
*
* @param indexName 索引名称
* @param id 文档id
* @return 是否删除成功
*/
public static boolean deleteDocument(String indexName, String id) {
try {
DeleteRequest request = new DeleteRequest(indexName, id);
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
return response.getResult() == DocWriteResponse.Result.DELETED;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 获取文档
*
* @param indexName 索引名称
* @param id 文档id
* @return 文档内容
*/
public static Map<String, Object> getDocument(String indexName, String id) {
try {
GetRequest request = new GetRequest(indexName, id);
GetResponse response = client.get(request, RequestOptions.DEFAULT);
if (response.isExists()) {
return response.getSourceAsMap();
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 搜索文档
*
* @param indexName 索引名称
* @param query 查询条件
* @param size 返回数量
* @return 搜索结果
*/
public static SearchHit[] search(String indexName, String query, int size) {
try {
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.queryStringQuery(query));
builder.size(size);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
if (response.status() == RestStatus.OK) {
return response.getHits().getHits();
} else {
return new SearchHit[0];
}
} catch (IOException e) {
e.printStackTrace();
return new SearchHit[0];
}
}
/**
* 异步搜索文档
*
* @param indexName 索引名称
* @param query 查询条件
* @param size 返回数量
* @param listener 异步回调
*/
public static void searchAsync(String indexName, String query, int size, ActionListener<SearchResponse> listener) {
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.queryStringQuery(query));
builder.size(size);
request.source(builder);
client.searchAsync(request, RequestOptions.DEFAULT, listener);
}
/**
* 关闭客户端连接
*/
public static void close() {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个工具类提供了创建索引、删除索引、新增文档、删除文档、获取文档、搜索文档等基本操作。其中,`RestHighLevelClient` 用于操作 Elasticsearch,`RestClientBuilder` 用于创建 `RestHighLevelClient` 实例,`HttpHost` 表示 Elasticsearch 的主机和端口,可以根据实际情况修改。
Elasticsearch 7.17的中文文档
Elasticsearch 7.17的中文文档可以在Elasticsearch官方网站上找到:https://www.elastic.co/guide/cn/elasticsearch/reference/current/index.html
该文档包含了Elasticsearch的各种功能和API的详细说明,包括搜索、聚合、索引、管理等方面的内容,适合初学者和有一定经验的开发人员阅读。同时,官方文档也提供了丰富的示例代码和实践建议,方便开发者快速上手使用Elasticsearch。
阅读全文