|
@@ -45,9 +45,13 @@ import org.elasticsearch.index.query.TermQueryBuilder;
|
|
|
import org.elasticsearch.index.query.WildcardQueryBuilder;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
+import org.elasticsearch.search.aggregations.AggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.Aggregations;
|
|
|
import org.elasticsearch.search.aggregations.BucketOrder;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.ParsedAvg;
|
|
@@ -440,11 +444,12 @@ public class ElasticSearchClient {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 聚合函数查询
|
|
|
*
|
|
|
- * @param indexName
|
|
|
+ * @param indexName 索引名
|
|
|
* @param aggsType
|
|
|
- * @param bucketName
|
|
|
- * @param metricName 需要统计分析的字段
|
|
|
+ * @param bucketName 分组字段
|
|
|
+ * @param metricName 需要统计分析的字段
|
|
|
* @param equalsCondition
|
|
|
* @param rangeCondition
|
|
|
* @return
|
|
@@ -465,13 +470,13 @@ public class ElasticSearchClient {
|
|
|
if (AggsType.count == aggsType) {
|
|
|
aggregation.subAggregation(AggregationBuilders.count(me).field(metricName)).size(10000);
|
|
|
} else if (AggsType.min == aggsType) {
|
|
|
- aggregation.subAggregation(AggregationBuilders.min(me).field(metricName)).size(Integer.MAX_VALUE);
|
|
|
+ aggregation.subAggregation(AggregationBuilders.min(me).field(metricName)).size(10000);
|
|
|
} else if (AggsType.max == aggsType) {
|
|
|
- aggregation.subAggregation(AggregationBuilders.max(me).field(metricName)).size(Integer.MAX_VALUE);
|
|
|
+ aggregation.subAggregation(AggregationBuilders.max(me).field(metricName)).size(10000);
|
|
|
} else if (AggsType.sum == aggsType) {
|
|
|
- aggregation.subAggregation(AggregationBuilders.sum(me).field(metricName)).size(Integer.MAX_VALUE);
|
|
|
+ aggregation.subAggregation(AggregationBuilders.sum(me).field(metricName)).size(10000);
|
|
|
} else if (AggsType.avg == aggsType) {
|
|
|
- aggregation.subAggregation(AggregationBuilders.avg(me).field(metricName)).size(Integer.MAX_VALUE);
|
|
|
+ aggregation.subAggregation(AggregationBuilders.avg(me).field(metricName)).size(10000);
|
|
|
}
|
|
|
sourceBuilder.aggregation(aggregation);
|
|
|
|
|
@@ -528,6 +533,95 @@ public class ElasticSearchClient {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 日期直方图聚合
|
|
|
+ * @param indexName
|
|
|
+ * @param aggsType
|
|
|
+ * @param bucketName
|
|
|
+ * @param metricName
|
|
|
+ * @param equalsCondition
|
|
|
+ * @param rangeCondition
|
|
|
+ * @param interval
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<Object, Object> dateHistogramAggs(String indexName,
|
|
|
+ AggsType aggsType,
|
|
|
+ String bucketName,
|
|
|
+ String metricName,
|
|
|
+ Map<String, Object> equalsCondition,
|
|
|
+ Map<String, Object> rangeCondition,
|
|
|
+ DateHistogramInterval interval) {
|
|
|
+ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
+
|
|
|
+ String by = "by_" + bucketName.replaceAll(keyword, "");
|
|
|
+ String me = aggsType.toString() + "_" + metricName.replaceAll(keyword, "");
|
|
|
+ AggregationBuilder aggregation = AggregationBuilders.dateHistogram(by).field(bucketName).dateHistogramInterval(interval);
|
|
|
+
|
|
|
+ if (AggsType.count == aggsType) {
|
|
|
+ aggregation.subAggregation(AggregationBuilders.count(me).field(metricName));
|
|
|
+ } else if (AggsType.min == aggsType) {
|
|
|
+ aggregation.subAggregation(AggregationBuilders.min(me).field(metricName));
|
|
|
+ } else if (AggsType.max == aggsType) {
|
|
|
+ aggregation.subAggregation(AggregationBuilders.max(me).field(metricName));
|
|
|
+ } else if (AggsType.sum == aggsType) {
|
|
|
+ aggregation.subAggregation(AggregationBuilders.sum(me).field(metricName));
|
|
|
+ } else if (AggsType.avg == aggsType) {
|
|
|
+ aggregation.subAggregation(AggregationBuilders.avg(me).field(metricName));
|
|
|
+ }
|
|
|
+ sourceBuilder.aggregation(aggregation);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ sourceBuilder.query(boolQueryBuilder);
|
|
|
+ sourceBuilder.size(0);
|
|
|
+
|
|
|
+ // 执行查询
|
|
|
+ SearchResponse response = executeSearch(indexName, sourceBuilder);
|
|
|
+ // 解析结果
|
|
|
+ Aggregations aggregations = response.getAggregations();
|
|
|
+ ParsedDateHistogram agg = aggregations.get(by);
|
|
|
+ Map<Object, Object> map = new LinkedHashMap<>();
|
|
|
+ for (Histogram.Bucket bucket : agg.getBuckets()) {
|
|
|
+ if (AggsType.count == aggsType) {
|
|
|
+ ValueCount count = bucket.getAggregations().get(me);
|
|
|
+ long value = count.getValue();
|
|
|
+ map.put(bucket.getKey(), value);
|
|
|
+ } else if (AggsType.min == aggsType) {
|
|
|
+ ParsedMin min = bucket.getAggregations().get(me);
|
|
|
+ double value = min.getValue();
|
|
|
+ map.put(bucket.getKey(), value);
|
|
|
+ } else if (AggsType.max == aggsType) {
|
|
|
+ ParsedMax max = bucket.getAggregations().get(me);
|
|
|
+ double value = max.getValue();
|
|
|
+ map.put(bucket.getKey(), value);
|
|
|
+ } else if (AggsType.sum == aggsType) {
|
|
|
+ ParsedSum sum = bucket.getAggregations().get(me);
|
|
|
+ double value = sum.getValue();
|
|
|
+ map.put(bucket.getKey(), value);
|
|
|
+ } else if (AggsType.avg == aggsType) {
|
|
|
+ ParsedAvg avg = bucket.getAggregations().get(me);
|
|
|
+ double value = avg.getValue();
|
|
|
+ map.put(bucket.getKey(), value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* @param boolQueryBuilder
|
|
|
* @param key
|
|
|
* @param value
|