import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.search.SearchHit;
@Service
public class ElasticSearchService {
@Autowired
private RestHighLevelClient client;
// 查询
public SearchResponse search(String index, String jsonQuery) throws IOException {
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.source(SearchSourceBuilder.fromJson(jsonQuery));
return client.search(searchRequest, RequestOptions.DEFAULT);
}
// 新增
public IndexResponse add(String index, String jsonSource) throws IOException {
IndexRequest indexRequest = new IndexRequest(index);
indexRequest.source(jsonSource, XContentType.JSON);
return client.index(indexRequest, RequestOptions.DEFAULT);
}
// 删除
public DeleteResponse delete(String index, String id) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest(index, id);
return client.delete(deleteRequest, RequestOptions.DEFAULT);
}
// 更新
public UpdateResponse update(String index, String id, String jsonDoc) throws IOEx
评论已关闭