给我写个ElasticsearchClient的工具类
时间: 2023-12-20 19:57:17 浏览: 133
好的,以下是一个简单的ElasticsearchClient工具类,你可以根据自己的需求进行修改和扩展:
```java
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class ElasticsearchClientUtil {
private static final String INDEX_NAME = "my_index";
private static final String TYPE_NAME = "my_type";
private RestHighLevelClient client;
public ElasticsearchClientUtil(String host, int port) {
client = new RestHighLevelClient(
RestClient.builder(new HttpHost(host, port, "http")));
}
public void close() throws IOException {
client.close();
}
public void indexDocument(String id, String jsonString) throws IOException {
IndexRequest indexRequest = new IndexRequest(INDEX_NAME, TYPE_NAME, id)
.source(jsonString, XContentType.JSON);
client.index(indexRequest, RequestOptions.DEFAULT);
}
public void indexDocument(String id, XContentBuilder source) throws IOException {
IndexRequest indexRequest = new IndexRequest(INDEX_NAME, TYPE_NAME, id).source(source);
client.index(indexRequest, RequestOptions.DEFAULT);
}
public BulkResponse bulkIndexDocuments(List<IndexRequest> requests) throws IOException {
BulkRequest bulkRequest = new BulkRequest();
for (IndexRequest request : requests) {
bulkRequest.add(request);
}
return client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
public SearchResponse search(QueryBuilder queryBuilder, int from, int size) throws IOException {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.query(queryBuilder)
.from(from)
.size(size);
SearchRequest searchRequest = new SearchRequest(INDEX_NAME).source(sourceBuilder);
return client.search(searchRequest, RequestOptions.DEFAULT);
}
public static void main(String[] args) throws IOException {
ElasticsearchClientUtil clientUtil = new ElasticsearchClientUtil("localhost", 9200);
String jsonString = "{\"name\": \"John Doe\", \"age\": 25}";
XContentBuilder source = XContentFactory.jsonBuilder()
.startObject()
.field("name", "John Doe")
.field("age", 25)
.endObject();
clientUtil.indexDocument("1", jsonString);
clientUtil.indexDocument("2", source);
clientUtil.close();
}
}
```
这个工具类包含了一些基本的 Elasticsearch 操作,包括:
- 创建Elasticsearch客户端:使用给定的主机和端口创建RestHighLevelClient实例,这是与Elasticsearch进行交互的主要入口。
- 关闭Elasticsearch客户端:关闭RestHighLevelClient实例。
- 索引文档:将JSON字符串或XContentBuilder对象索引到指定的Elasticsearch索引和类型中。
- 批量索引文档:将多个索引请求打包成单个批量请求,并将其发送到Elasticsearch进行索引。
- 搜索文档:使用给定的QueryBuilder对象构建查询,然后使用SearchSourceBuilder构建搜索请求,最后使用RestHighLevelClient执行搜索请求并返回响应。
你可以根据自己的需求扩展这个工具类,添加更多的操作,例如删除文档、更新文档等等。
阅读全文