|
@@ -27,6 +27,8 @@ import org.elasticsearch.action.update.UpdateResponse;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
import org.elasticsearch.client.RestClient;
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
+import org.elasticsearch.client.core.CountRequest;
|
|
|
+import org.elasticsearch.client.core.CountResponse;
|
|
|
import org.elasticsearch.client.indices.CreateIndexRequest;
|
|
|
import org.elasticsearch.client.indices.CreateIndexResponse;
|
|
|
import org.elasticsearch.client.indices.GetIndexRequest;
|
|
@@ -35,14 +37,7 @@ import org.elasticsearch.common.unit.DistanceUnit;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
import org.elasticsearch.core.TimeValue;
|
|
|
-import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
-import org.elasticsearch.index.query.GeoDistanceQueryBuilder;
|
|
|
-import org.elasticsearch.index.query.MatchQueryBuilder;
|
|
|
-import org.elasticsearch.index.query.PrefixQueryBuilder;
|
|
|
-import org.elasticsearch.index.query.QueryBuilders;
|
|
|
-import org.elasticsearch.index.query.RangeQueryBuilder;
|
|
|
-import org.elasticsearch.index.query.TermQueryBuilder;
|
|
|
-import org.elasticsearch.index.query.WildcardQueryBuilder;
|
|
|
+import org.elasticsearch.index.query.*;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilder;
|
|
@@ -73,6 +68,7 @@ import org.springframework.util.CollectionUtils;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
@@ -427,7 +423,7 @@ public class ElasticSearchClient {
|
|
|
}
|
|
|
|
|
|
//执行查询
|
|
|
- SearchResponse response = executeSearch(indexName, sourceBuilder);
|
|
|
+ SearchResponse response = executeSearch(sourceBuilder, indexName);
|
|
|
SearchHits searchHits = response.getHits();
|
|
|
SearchHit[] hits = searchHits.getHits();
|
|
|
totalNum = searchHits.getTotalHits().value;
|
|
@@ -501,7 +497,7 @@ public class ElasticSearchClient {
|
|
|
sourceBuilder.size(0);
|
|
|
|
|
|
// 执行查询
|
|
|
- SearchResponse response = executeSearch(indexName, sourceBuilder);
|
|
|
+ SearchResponse response = executeSearch(sourceBuilder, indexName);
|
|
|
// 解析结果
|
|
|
Aggregations aggregations = response.getAggregations();
|
|
|
Terms terms = aggregations.get(by);
|
|
@@ -534,6 +530,7 @@ public class ElasticSearchClient {
|
|
|
|
|
|
/**
|
|
|
* 日期直方图聚合
|
|
|
+ *
|
|
|
* @param indexName
|
|
|
* @param aggsType
|
|
|
* @param bucketName
|
|
@@ -590,7 +587,7 @@ public class ElasticSearchClient {
|
|
|
sourceBuilder.size(0);
|
|
|
|
|
|
// 执行查询
|
|
|
- SearchResponse response = executeSearch(indexName, sourceBuilder);
|
|
|
+ SearchResponse response = executeSearch(sourceBuilder, indexName);
|
|
|
// 解析结果
|
|
|
Aggregations aggregations = response.getAggregations();
|
|
|
ParsedDateHistogram agg = aggregations.get(by);
|
|
@@ -622,6 +619,38 @@ public class ElasticSearchClient {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 查询数量(跨索引)
|
|
|
+ * @param equalsCondition
|
|
|
+ * @param rangeCondition
|
|
|
+ * @param indexName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public long count(Map<String, Object> equalsCondition,
|
|
|
+ Map<String, Object> rangeCondition,
|
|
|
+ String... indexName) {
|
|
|
+ BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
|
|
+ // 某一field=具体的值; 也可以某一field 的值 in 具体定义集合里的值
|
|
|
+ if (null != equalsCondition && !equalsCondition.isEmpty()) {
|
|
|
+ for (Map.Entry<String, Object> entry : equalsCondition.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ //由于我创建索引的时候使用字符串不分词使用的.keyword类型
|
|
|
+ if (key.endsWith("_s")) {
|
|
|
+ queryValueBuild(boolQueryBuilder, key + ".keyword", entry.getValue());
|
|
|
+ } else {
|
|
|
+ queryValueBuild(boolQueryBuilder, key, entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //范围查询
|
|
|
+ if (null != rangeCondition && !rangeCondition.isEmpty()) {
|
|
|
+ rangeValueBuild(boolQueryBuilder, rangeCondition);
|
|
|
+ }
|
|
|
+ // 执行查询
|
|
|
+ CountResponse response = executeCountSearch(boolQueryBuilder, indexName);
|
|
|
+ return response.getCount();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* @param boolQueryBuilder
|
|
|
* @param key
|
|
|
* @param value
|
|
@@ -751,15 +780,15 @@ public class ElasticSearchClient {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param indexName 对应的es的index名
|
|
|
* @param sourceBuilder
|
|
|
+ * @param indexName 对应的es的index名
|
|
|
* @return
|
|
|
* @Description 执行查询
|
|
|
*/
|
|
|
- private static SearchResponse executeSearch(String indexName, SearchSourceBuilder sourceBuilder) {
|
|
|
+ private static SearchResponse executeSearch(SearchSourceBuilder sourceBuilder, String... indexName) {
|
|
|
// 获取不同系统的换行符
|
|
|
String lineSeparator = System.lineSeparator();
|
|
|
- log.info(lineSeparator + "ES查询:index:" + indexName + lineSeparator + "search:" + sourceBuilder.toString() + lineSeparator);
|
|
|
+ log.info(lineSeparator + "ES查询:index:" + Arrays.toString(indexName) + lineSeparator + "search:" + sourceBuilder.toString() + lineSeparator);
|
|
|
SearchRequest searchRequest = new SearchRequest(indexName);
|
|
|
SearchResponse response = null;
|
|
|
searchRequest.source(sourceBuilder);
|
|
@@ -773,6 +802,23 @@ public class ElasticSearchClient {
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
+ private static CountResponse executeCountSearch(QueryBuilder sourceBuilder, String... indexName) {
|
|
|
+ // 获取不同系统的换行符
|
|
|
+ String lineSeparator = System.lineSeparator();
|
|
|
+ log.info(lineSeparator + "ES查询:index:" + Arrays.toString(indexName) + lineSeparator + "search:" + sourceBuilder.toString() + lineSeparator);
|
|
|
+ CountRequest countRequest = new CountRequest(indexName);
|
|
|
+ CountResponse response = null;
|
|
|
+ countRequest.query(sourceBuilder);
|
|
|
+ try {
|
|
|
+ response = client.count(countRequest, RequestOptions.DEFAULT);
|
|
|
+ log.info("search status:{}, totalNum:{}", response.status(), response.getCount());
|
|
|
+ } catch (IOException e) {
|
|
|
+ //异常处理,实际业务中是需要根据需求具体处理,自定义异常捕获
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 索引创建前,判断索引是否存在
|
|
|
*/
|