ElasticSearch: 聚合搜索

java标志
image-3375

Java(Spring Data ElasticSearch):

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
  var nativeSearchQueryBuilder = new NativeSearchQueryBuilder().withFields("id");
    var queryBuilder = QueryBuilders.boolQuery();
    queryBuilder.must(QueryBuilders.termQuery("phone", "132xxxxxxxdxx"));
    var groupName = "group_aIndex";
    var query =
        nativeSearchQueryBuilder
            .withQuery(queryBuilder)
            .addAggregation(
                AggregationBuilders.terms(groupName)
                    .field("aIndex")
                    .subAggregation(
                        AggregationBuilders.count("count_tId").field("tId")))
            .build();
    var searchHits = elasticsearchOperations.search(query, ESInfo.class);
    var result = searchHits.getAggregations();
    if (result != null) {
      var aggregationMap = result.asMap();
      var parsedLongTerms = (ParsedLongTerms) aggregationMap.get(groupName);
      return parsedLongTerms.getBuckets().stream()
          .map(
              item ->{
                System.out.println(item.getKeyAsNumber().intValue()+","+item.getDocCount())
              }              
          .toList();
    }

发表评论

您的电子邮箱地址不会被公开。

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据