ElasticSearch常用API
package com.chuang.until;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
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.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
@Component
public class ESUtil {
@Autowired
RestHighLevelClient restHighLevelClient;
public Boolean CreatemyIndex(String index) throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
return createIndexResponse.isAcknowledged();
}
public Boolean ExistIndex(String index) throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest(index);
return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
}
public Boolean DeleteIndex(String index) throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
return delete.isAcknowledged();
}
public RestStatus AddDocument(String index, String id, Object object) throws IOException {
IndexRequest indexRequest = new IndexRequest(index);
indexRequest.id(id);
indexRequest.timeout(TimeValue.timeValueSeconds(2));
indexRequest.source(JSON.toJSONString(object), XContentType.JSON);
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
RestStatus status = indexResponse.status();
return status;
}
public boolean DocumentIsExists(String index,String id) throws IOException {
GetRequest getRequest = new GetRequest(index, id);
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
return restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
}
public Map<String, Object> GetDocument(String index,String id)throws IOException {
GetRequest getRequest = new GetRequest(index, id);
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
return documentFields.getSource();
}
public GetResponse GetAllDocument(String index, String id)throws IOException {
GetRequest getRequest = new GetRequest(index, id);
return restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
}
public RestStatus UpdateDocument(String index, String id, Object updateobject) throws IOException {
UpdateRequest updateRequest = new UpdateRequest(index, id);
updateRequest.timeout("2s");
updateRequest.doc(JSON.toJSONString(updateobject),XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
return updateResponse.status();
}
public RestStatus DeleteDocument(String index, String id) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest(index,id);
deleteRequest.timeout("1s");
DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
return delete.status();
}
public RestStatus BulkInsertDocument(ArrayList bulklist, String index) throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("120s");
for (int i = 0; i < bulklist.size(); i++) {
bulkRequest.add(new IndexRequest(index)
.id(""+(i+1))
.source(JSON.toJSONString(bulklist.get(i)),XContentType.JSON)
);
}
BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
return bulkResponse.status();
}
public ArrayList<Map<String, Object>> SearchReques() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder queryBuilder = QueryBuilders.termQuery("name", "chuang");
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("title");
highlightBuilder.requireFieldMatch(false);
highlightBuilder.preTags("<span style='color:red'>");
highlightBuilder.postTags("</span>");
searchSourceBuilder.highlighter(highlightBuilder);
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.from();
searchSourceBuilder.size();
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
ArrayList<Map<String,Object>> resultlist = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits().getHits()) {
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField title = highlightFields.get("title");
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
if(title!=null){
Text[] fragments = title.fragments();
String n_title = "";
for (Text text : fragments) {
n_title += text;
}
sourceAsMap.put("title",n_title);
}
resultlist.add(sourceAsMap);
System.out.println(sourceAsMap);
}
return resultlist;
}
}
package com.chuang.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ESClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("192.168.88.129", 9200, "http")
)
);
System.out.println(client);
return client;
}
}