Procházet zdrojové kódy

优化聚合查询

zhou-hao před 5 roky
rodič
revize
f77ea98fa0

+ 12 - 0
jetlinks-components/elasticsearch-component/src/main/java/org/jetlinks/community/elastic/search/aggreation/bucket/AggregationResponseHandle.java

@@ -9,6 +9,7 @@ import org.elasticsearch.search.aggregations.metrics.max.Max;
 import org.elasticsearch.search.aggregations.metrics.min.Min;
 import org.elasticsearch.search.aggregations.metrics.stats.Stats;
 import org.elasticsearch.search.aggregations.metrics.sum.Sum;
+import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount;
 import org.jetlinks.community.elastic.search.aggreation.metrics.MetricsResponseSingleValue;
 
 import java.util.List;
@@ -97,11 +98,22 @@ public class AggregationResponseHandle {
             bucket.setSum(sum(a));
         } else if (a instanceof Stats) {
             stats(bucket, a);
+        }  else if (a instanceof ValueCount) {
+            bucket.setValueCount(count(a));
         } else {
             throw new UnsupportedOperationException("不支持的聚合类型");
         }
     }
 
+    public static <A extends Aggregation> MetricsResponseSingleValue count(A a) {
+        ValueCount max = (ValueCount) a;
+        return MetricsResponseSingleValue.builder()
+            .value(max.getValue())
+            .name(a.getName())
+            .valueAsString(max.getValueAsString())
+            .build();
+    }
+
     public static <A extends Aggregation> MetricsResponseSingleValue avg(A a) {
         Avg avg = (Avg) a;
         return MetricsResponseSingleValue.builder()

+ 9 - 13
jetlinks-components/elasticsearch-component/src/main/java/org/jetlinks/community/elastic/search/aggreation/bucket/Bucket.java

@@ -44,31 +44,27 @@ public class Bucket {
 
     private List<Bucket> buckets;
 
+    private double toNumber(double number) {
+        return (Double.isInfinite(number) || Double.isNaN(number)) ? 0 : number;
+    }
+
     public Map<String, Number> toMap() {
         Map<String, Number> map = new HashMap<>();
         if (this.sum != null) {
-            map.put(sum.getName(), sum.getValue());
+            map.put(sum.getName(), toNumber(sum.getValue()));
         }
         if (this.valueCount != null) {
-            map.put(valueCount.getName(), valueCount.getValue());
+            map.put(valueCount.getName(), toNumber(valueCount.getValue()));
         }
         if (this.avg != null) {
-            map.put(avg.getName(), avg.getValue());
+            map.put(avg.getName(), toNumber(avg.getValue()));
         }
         if (this.min != null) {
-            map.put(min.getName(), min.getValue());
+            map.put(min.getName(), toNumber(min.getValue()));
         }
         if (this.max != null) {
-            map.put(max.getName(), max.getValue());
+            map.put(max.getName(), toNumber(max.getValue()));
         }
-//
-//        if (this.getBuckets() != null) {
-//            bucketFlatMap(this.getBuckets(), map);
-//        }
         return map;
     }
-
-//    private void bucketFlatMap(List<Bucket> buckets, Map<String, Number> map) {
-//        buckets.forEach(bucket -> map.putAll(bucket.toMap()));
-//    }
 }