elasticsearchTemplate的聚合查询
直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.should(QueryBuilders.termQuery("字段", "值"));
TermsAggregationBuilder agg = AggregationBuilders.terms("聚合名称,自定义,取出时会用到").field("聚合字段").size(100);
SearchQuery build = new NativeSearchQueryBuilder() .withQuery(queryBuilder) .addAggregation(agg) .withPageable(PageRequest.of(0, 1)) .build();
AggregatedPage<TestEntity> testEntities = elasticsearchTemplate.queryForPage(build, TestEntity.class);
Aggregations entitiesAggregations = testEntities.getAggregations(); Terms terms = (Terms)propertyInfoEntities.getAggregation("聚合名称,之前自定义的");
for (Terms.Bucket bucket : terms.getBuckets()) { String keyAsString = bucket.getKeyAsString(); long docCount = bucket.getDocCount(); }
|
多字段聚合:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.should(QueryBuilders.termQuery("字段", "值"));
TermsAggregationBuilder agg1 = AggregationBuilders.terms("聚合名称2").field(聚合字段2).size(100)
TermsAggregationBuilder agg = AggregationBuilders.terms("聚合名称1").field("聚合字段1").size(100).subAggregation(agg1)
SearchQuery build = new NativeSearchQueryBuilder() .withQuery(queryBuilder) .addAggregation(agg) .withPageable(PageRequest.of(0, 1)) .build();
AggregatedPage<TestEntity> testEntities = elasticsearchTemplate.queryForPage(build, TestEntity.class);
Aggregations entitiesAggregations = testEntities.getAggregations(); Terms terms = (Terms)propertyInfoEntities.getAggregation("聚合名称");
for (Terms.Bucket bucket : terms.getBuckets()) { String keyAsString = bucket.getKeyAsString(); long docCount = bucket.getDocCount(); Aggregations aggregations = bucket.getAggregations(); Terms terms2 = aggregations.get("聚合名称2"); for (Terms.Bucket bucket1 : terms2.getBuckets()) { String keyAsString2 = bucket.getKeyAsString(); long docCount2 = bucket.getDocCount(); } }
|
多个字段一次性聚合:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.should(QueryBuilders.termQuery("字段", "值"));
String script = "doc['" + 字段1 + "'].values +'/'+ doc['" + 字段2 + "'].values +'/'+ doc['" + 字段3 + "'].values";
Script script1 = new Script(script);
TermsAggregationBuilder agg = AggregationBuilders.terms("聚合名称").script(script1).size(99999);
SearchQuery build = new NativeSearchQueryBuilder() .withQuery(queryBuilder) .addAggregation(agg) .withPageable(PageRequest.of(0, 1)) .build();
AggregatedPage<TestEntity> testEntities = elasticsearchTemplate.queryForPage(build, TestEntity.class);
Terms terms = (Terms) shopDbEntities.getAggregation("聚合名称");
for (Terms.Bucket bucket : terms.getBuckets()) { String keyAsString = bucket.getKeyAsString(); String[] split = keyAsString.split("/"); System.out.println("字段1"+split[0]); System.out.println("字段2"+split[1]); System.out.println("字段3"+split[2]); }
|
更多字段聚合按照以上类推,如果实在对于结构不理解可以将查询语句打印出来,将打印语句拿到Kibana之类的查询工具中查询看下结构,或者直接Debug看下数据结构,就比较好理解了