com.gitee.summer9102.develop.alibaba.elasticsearch.AliElasticsearchUtil Maven / Gradle / Ivy
package com.gitee.summer9102.develop.alibaba.elasticsearch;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class AliElasticsearchUtil {
/**
* 插入更新
*/
public static UpdateResponse upsert(AliElasticsearch aliElasticsearch, String index, String type, String id, XContentBuilder builder) throws IOException {
IndexRequest indexRequest = new IndexRequest(index, type, id)
.source(builder);
UpdateRequest updateRequest = new UpdateRequest(index, type, id)
.doc(builder).upsert(indexRequest);
UpdateResponse response = aliElasticsearch.getHighClient().update(updateRequest, RequestOptions.DEFAULT);
return response;
}
/**
* 删除
*/
public static DeleteResponse delete(AliElasticsearch aliElasticsearch, String index, String type, String id) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest(index, type, id);
DeleteResponse response = aliElasticsearch.getHighClient().delete(deleteRequest, RequestOptions.DEFAULT);
return response;
}
/**
* 查询
*/
public static SearchResponse search(AliElasticsearch aliElasticsearch, String index, String type, SearchSourceBuilder searchSourceBuilder) throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(index);
searchRequest.types(type);
searchRequest.source(searchSourceBuilder);
SearchResponse response = aliElasticsearch.getHighClient().search(searchRequest, RequestOptions.DEFAULT);
return response;
}
}