1.使用时调用该类中的方法:
package cn.com.cewell.search.esBase;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpHost;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
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.action.search.SearchType;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.*;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
import org.elasticsearch.search.aggregations.metrics.cardinality.InternalCardinality;
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import java.lang.reflect.Field;
import java.util.*;
public class EsBaseUtils {
private static int TIME_OUT = 5 * 60 * 1000;
private static RestHighLevelClient restHighLevelClient = null;
private static String host = "127.0.0.1";
private static int port = 9200;
//获取客户端
private static RestHighLevelClient getClient() {
if (restHighLevelClient == null) {
buildClient();
}
return restHighLevelClient;
}
//构建客户端
private static void buildClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
.setMaxRetryTimeoutMillis(TIME_OUT)
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
restHighLevelClient = new RestHighLevelClient(builder);
System.out.println(">>>>>>buildClient>>>host:" + host + " port:" + port);
}
//判断索引是否存在
public static boolean indexExists(String index) {
GetIndexRequest request = new GetIndexRequest();
request.indices(index);
try {
return getClient().indices().exists(request, RequestOptions.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
System.out.println(">>>>>>indexExists>>>index isExists fail! index=" + index);
return false;
}
}
//删除索引
public static void deleteIndex(String index) {
DeleteIndexRequest request = new DeleteIndexRequest(index);
request.indices(index);
try {
getClient().indices().delete(request, RequestOptions.DEFAULT);
System.out.println(">>>>>>deleteIndex>>>delete index success! index=" + index);
} catch (Exception e) {
e.printStackTrace();
System.out.println(">>>>>>deleteIndex>>>delete index fail! index=" + index);
}
}
//创建索引
public static boolean createIndexAndMapping(String index, String type, Class> clazz) {
if (indexExists(index)) {
// 存在就删除重新创建
deleteIndex(index);
}
try {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
CreateIndexResponse createIndexResponse = getClient().indices().create(createIndexRequest, RequestOptions.DEFAULT);
if (!createIndexResponse.isAcknowledged()) {
System.out.println(">>>>>>createIndexAndMapping>>>create index error! index=" + index);
return false;
}
PutMappingRequest putMapping = Requests.putMappingRequest(index).type(type).source(createMapping(clazz));
AcknowledgedResponse response = getClient().indices().putMapping(putMapping, RequestOptions.DEFAULT);
if (!response.isAcknowledged()) {
System.out.println(">>>>>>createIndexAndMapping>>>create mapping error! index=" + index+" type="+type);
return false;
}
System.out.println(">>>>>>createIndexAndMapping>>>create mapping success! index=" + index+" type="+type);
} catch (Exception e) {
e.printStackTrace();
System.out.println(">>>>>>createIndexAndMapping>>>create error! index=" + index+" type="+type);
return false;
}
return true;
}
//查询数据-返回数据
public static JSONArray queryList(String index, String type, QueryRule rule) {
JSONArray jsonArray = new JSONArray();
try {
//拼接查询条件
SearchSourceBuilder sourceBuilder = search(rule);
if (null == sourceBuilder) {
return jsonArray;
}
SearchRequest searchRequest = new SearchRequest(index).types(type)
.searchType(SearchType.QUERY_THEN_FETCH)
.source(sourceBuilder);
SearchResponse response = getClient().search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
jsonArray.add(hit.getSourceAsMap());
}
System.out.println(">>>>>>queryList>>>size:" + jsonArray.size() + " jsonArray:" + jsonArray);
} catch (Exception e) {
e.printStackTrace();
System.out.println(">>>>>>queryList>>>size:" + jsonArray.size() + " 失败:" + e.getMessage());
}
return jsonArray;
}
//查询数据-聚合
public static JSONArray queryListAgg(String index, String type, QueryRule rule,List groupFields){
JSONArray jsonArray = new JSONArray();
try {
//拼接普通查询条件
SearchSourceBuilder sourceBuilder = search(rule);
if (null == sourceBuilder) {
return jsonArray;
}
//拼接聚合条件
TermsAggregationBuilder termsBuilder = AggregationBuilders.terms(groupFields.get(0)).field(groupFields.get(0)).size(10000);
searchAgg(termsBuilder,groupFields,0);
sourceBuilder.aggregation(termsBuilder);
//查询
SearchRequest searchRequest = new SearchRequest(index).types(type)
.searchType(SearchType.QUERY_THEN_FETCH)
.source(sourceBuilder);
SearchResponse response = getClient().search(searchRequest, RequestOptions.DEFAULT);
//解析聚合结果
ArrayList
2.使用方法
@RequestMapping("/testQuery")
public void list(HttpServletRequest request, HttpServletResponse response){
System.out.println("测试查询ES数据");
QueryRule rule = new QueryRule();
rule.add(Restrictions.like("file_id", "123"))
.add(Restrictions.ge("start_time","2021-04-01 00:00:24"))
.add(Restrictions.le("start_time","2021-04-01 23:00:24"))
.addOrder("start_time").orderDesc();
EsBaseUtils.queryList("test", "type", rule);
}
3.查询前置条件拼装
package cn.com.cewell.search.esBase;
import org.elasticsearch.search.sort.SortOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class QueryRule {
private int firstResult;
private int maxResults;
List criterions = new ArrayList<>();
private List orderPropertyNames = new ArrayList<>();
private SortOrder sortMode = SortOrder.ASC;
public QueryRule add(QueryCriterion criterion) {
criterions.add(criterion);
return this;
}
public QueryRule clear(QueryCriterion criterion) {
criterions.clear();
firstResult = -1;
maxResults = 0;
return this;
}
public QueryRule addOrder(String ...name) {
orderPropertyNames.addAll(Arrays.asList(name));
return this;
}
public QueryRule orderDesc() {
this.sortMode = SortOrder.DESC;
return this;
}
public int getFirstResult() {
return firstResult;
}
public QueryRule setFirstResult(int firstResult) {
this.firstResult = firstResult;
return this;
}
public int getMaxResults() {
return maxResults;
}
public QueryRule setMaxResults(int maxResults) {
this.maxResults = maxResults;
return this;
}
public List getCriterions() {
return criterions;
}
public List getOrderPropertyNames() {
return orderPropertyNames;
}
public SortOrder getSortMode() {
return sortMode;
}
}
4.封装不同查询条件
package cn.com.cewell.search.esBase;
import java.util.Collection;
public class Restrictions {
public static QueryCriterion eq(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, QueryCriterion.eq);
}
public static QueryCriterion eq(String propertyName, Object ...value) {
return new ValuesExpression(propertyName, value);
}
public static QueryCriterion like(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, QueryCriterion.like);
}
public static QueryCriterion ne(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, QueryCriterion.ne);
}
public static QueryCriterion gt(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, QueryCriterion.gt);
}
public static QueryCriterion lt(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, QueryCriterion.lt);
}
public static QueryCriterion ge(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, QueryCriterion.ge);
}
public static QueryCriterion le(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, QueryCriterion.le);
}
public static QueryCriterion matchQuery(String propertyName, Object value) {
return new SimpleExpression(propertyName, value, QueryCriterion.matchQuery);
}
public static QueryCriterion in(String propertyName, Collection> values) {
return new InExpression(propertyName, values);
}
public static QueryCriterion multipleFileds(String value, String ...propertyNames) {
return new MultipleFieldExpression(value, propertyNames);
}
}
5.常量
package cn.com.cewell.search.esBase;
import org.elasticsearch.index.query.QueryBuilder;
public abstract class QueryCriterion {
public abstract QueryBuilder getBuilder();
public static final String eq = "=";
public static final String like = "like";
public static final String ne = "<>";
public static final String gt = ">";
public static final String lt = "<";
public static final String ge = ">=";
public static final String le = "<=";
public static final String matchQuery = "matchQuery";
}
6.查询条件拼接
package cn.com.cewell.search.esBase;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
public class SimpleExpression extends QueryCriterion {
private String propertyName;
private Object value;
private String op;
public SimpleExpression(String propertyName, Object value, String op) {
this.propertyName = propertyName;
this.value = value;
this.op = op;
}
public String getPropertyName() {
return propertyName;
}
public Object getValue() {
return value;
}
public String getOp() {
return op;
}
@Override
public QueryBuilder getBuilder() {
QueryBuilder qb = null;
switch (op) {
case eq:
// qb = QueryBuilders.termQuery(propertyName, value);
//可能有多个值,此处的替换要根据实际数据格式调整
qb = QueryBuilders.regexpQuery(propertyName, value.toString().replaceAll(",", "|"));
break;
case like:
// qb = QueryBuilders.wildcardQuery(propertyName, "*"+value.toString()+"*");
//可能会有多个值
String[] split = value.toString().split(",");
String fileVal = "";
for (String s : split) {
fileVal+=".*"+s+".*|";
}
fileVal = fileVal.length() > 0 ? fileVal.substring(0, fileVal.length() - 1) : "";
if (!"".equals(fileVal)){
qb = QueryBuilders.regexpQuery(propertyName,fileVal);
}
break;
case ne:
break;
case gt:
qb = QueryBuilders.rangeQuery(propertyName).gt(value);
break;
case lt:
qb = QueryBuilders.rangeQuery(propertyName).lt(value);
break;
case ge:
qb = QueryBuilders.rangeQuery(propertyName).gte(value);
break;
case le:
qb = QueryBuilders.rangeQuery(propertyName).lte(value);
break;
case matchQuery:
qb = QueryBuilders.matchQuery(propertyName, value);
break;
}
return qb;
}
}
7.不同查询条件拼接---一次匹配多个值
package cn.com.cewell.search.esBase;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
public class ValuesExpression extends QueryCriterion {
private String propertyName;
private Object []values;
public ValuesExpression(String propertyName, Object ...values) {
this.propertyName = propertyName;
this.values = values;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public void setValues(Object[] values) {
this.values = values;
}
@Override
public QueryBuilder getBuilder() {
return QueryBuilders.termsQuery(propertyName, values);
}
}
8.不同查询条件拼接---一次匹配多个值
package cn.com.cewell.search.esBase;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import java.util.Collection;
public class InExpression extends QueryCriterion {
private String propertyName;
private Collection> values;
public InExpression(String propertyName, Collection> values) {
this.propertyName = propertyName;
this.values = values;
}
public String getPropertyName() {
return propertyName;
}
public Collection> getValues() {
return values;
}
@Override
public QueryBuilder getBuilder() {
return QueryBuilders.termsQuery(propertyName, values);
}
}
9.不同查询条件拼接---多字段匹配
package cn.com.cewell.search.esBase;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
public class MultipleFieldExpression extends QueryCriterion {
private String []propertyName;
private String value;
public MultipleFieldExpression(String value, String ...propertyName) {
this.propertyName = propertyName;
this.value = value;
}
public String[] getPropertyName() {
return propertyName;
}
public String getValue() {
return value;
}
@Override
public QueryBuilder getBuilder() {
return QueryBuilders.multiMatchQuery(value, propertyName);
}
}