فهرست منبع

es中直方图工具

459242451@qq.com 3 سال پیش
والد
کامیت
83c860c45b

+ 18 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/gas/So2Controller.java

@@ -28,6 +28,7 @@ import com.ruoyi.system.service.ISo2Service;
 import com.ruoyi.system.service.ISysDictTypeService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -221,4 +222,21 @@ public class So2Controller extends BaseController {
         return AjaxResult.success(result);
     }
 
+    @GetMapping("/timeStatic")
+    @ApiOperation("超标船舶统计-按时间")
+    public AjaxResult monthStatic(@RequestParam(required = false) String interval) {
+        if (StrUtil.isBlank(interval)) {
+            // 默认值为1个月
+            interval = "1M";
+        }
+        Map<Object, Object> aisAggs = client.dateHistogramAggs(ElasticConstants.AIS_ILLEGAL_SHIP, AggsType.count, "peakTime", "id", null, null, new DateHistogramInterval(interval));
+        Map<Object, Object> so2Aggs = client.dateHistogramAggs(ElasticConstants.SO2_ALERT, AggsType.count, "createTime", "id", null, null, new DateHistogramInterval(interval));
+        Map<Object, Object> heiyanAggs = client.dateHistogramAggs(ElasticConstants.HEIYAN_SHIP_RECOGNITION, AggsType.count, "snapTimeFmt", "id", null, null, new DateHistogramInterval(interval));
+        Map<String, Map<Object, Object>> result = new HashMap<>();
+        result.put("ais", aisAggs);
+        result.put("so2", so2Aggs);
+        result.put("black", heiyanAggs);
+        return AjaxResult.success(result);
+    }
+
 }

+ 101 - 7
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ElasticSearchClient.java

@@ -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