最近在做ES的aggregation,順便說一下收穫。
一:版本問題
dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${elasticserch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>${elasticserch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport-netty4-client</artifactId> <version>6.8.0</version> </dependency>
這是配置的版本,.version設置爲6.8 應用的es版本也是6.8
2.對於aggregation的數據解決
當應用SearchBuilder進行查詢後,應用管道流查詢出的數據特別繁瑣
在應用debug調試的時候關注了一下
SearchResponse srps = sr.setSource(sourceBuilder).get(); //處理結果 Map<String,Aggregation> aggMap =srps.getAggregations().asMap(); InternalDateHistogram userCountDay = (InternalDateHistogram)aggMap.get("userDayCount"); List<InternalDateHistogram.Bucket> list = userCountDay.getBuckets(); for (InternalDateHistogram.Bucket in:list){ Aggregations aggregations = in.getAggregations(); List<Aggregation> aggregations1= aggregations.asList(); for(Aggregation a:aggregations1){ Double value = ((InternalSimpleValue) (a)).getValue(); Long putIn = new Double(value).longValue(); countList.add(new EsUserCount(putIn,in.getKeyAsString())); } }
最後將管道中的數據去出,着實費了一大波的勁兒