java 编写elasticsearch的工具类,用于创建索引,查询,分页查询,高亮查询,删除,批量删除等等
时间: 2023-08-16 18:06:13 浏览: 108
当然可以!下面是一个简单的Java工具类,用于与Elasticsearch进行索引的创建、查询、分页查询、高亮查询、删除和批量删除等操作:
```java
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
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.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
public class ElasticsearchUtils {
private RestHighLevelClient client;
public ElasticsearchUtils(RestHighLevelClient client) {
this.client = client;
}
public void createIndex(String indexName, String documentId, String jsonString) throws IOException {
IndexRequest request = new IndexRequest(indexName).id(documentId).source(jsonString);
client.index(request, RequestOptions.DEFAULT);
}
public SearchResponse search(String indexName, String field, String keyword, int from, int size) throws IOException {
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchQuery(field, keyword));
sourceBuilder.from(from);
sourceBuilder.size(size);
request.source(sourceBuilder);
return client.search(request, RequestOptions.DEFAULT);
}
public SearchResponse searchWithHighlight(String indexName, String field, String keyword, int from, int size) throws IOException {
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchQuery(field, keyword));
sourceBuilder.from(from);
sourceBuilder.size(size);
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field(field);
highlightBuilder.requireFieldMatch(false);
highlightBuilder.preTags("<span style=\"color:red\">");
highlightBuilder.postTags("</span>");
sourceBuilder.highlighter(highlightBuilder);
request.source(sourceBuilder);
return client.search(request, RequestOptions.DEFAULT);
}
public SearchResponse searchWithSort(String indexName, String field, SortOrder sortOrder, int from, int size) throws IOException {
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.sort(SortBuilders.fieldSort(field).order(sortOrder));
sourceBuilder.from(from);
sourceBuilder.size(size);
request.source(sourceBuilder);
return client.search(request, RequestOptions.DEFAULT);
}
public void deleteDocument(String indexName, String documentId) throws IOException {
DeleteRequest request = new DeleteRequest(indexName).id(documentId);
client.delete(request, RequestOptions.DEFAULT);
}
public void bulkDeleteDocuments(String indexName, String... documentIds) throws IOException {
BulkRequest request = new BulkRequest();
for (String documentId : documentIds) {
request.add(new DeleteRequest(indexName).id(documentId));
}
client.bulk(request, RequestOptions.DEFAULT);
}
}
```
你可以通过该工具类的方法来进行索引的创建、查询、分页查询、高亮查询、删除和批量删除操作。记得在使用该工具类之前,你需要先创建一个 `RestHighLevelClient` 实例并传入构造方法中。希望这可以帮到你!如果有任何问题,请随时提问。
阅读全文