在Elasticsearch中仅获取聚合结果

huangapple go评论67阅读模式
英文:

Getting only aggregations results in elasticsearch

问题

我正在进行类似这样的聚合操作:

{
   "size": 0,
   "aggs": {
      "my_aggs": {
         "terms": {
            "field": "my_field"
         }
      }
   }
}

我想要获得仅聚合结果。因此,当我像下面这样设置 size=0 时,会出现错误 - 后来了解到这是我想要的结果数量(聚合结果)。那么,我如何做才能仅获得聚合结果(而不获取命中的文档结果)呢?

AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
         .terms("my_aggs")
         .field("my_field")
         .size(0) // 如何为我的目的设置 size?
         .order(BucketOrder.key(true));

此外,如果我获得数千个聚合结果,这个查询会将它们全部返回吗?还是只会应用默认的大小 10?如果不是,我如何知道应该设置聚合结果的 size。 <br><br><br>
编辑 我是这样添加我的聚合的:

SearchQuery searchQuery = new NativeSearchQueryBuilder()
         .withIndices(indexName)
         .withTypes(typeName)
         .withQuery(boolQueryBuilder)
         .addAggregation(aggregationBuilder)
         .build();

请提供帮助。

英文:

I am doing aggregation like this:

  {
   &quot;size&quot;:0,
   &quot;aggs&quot;:
    {
      &quot;my_aggs&quot;:
      {
         &quot;terms&quot;:
          {
            &quot;field&quot;:&quot;my_field&quot;
          }
      }
   }
}

I want to get only the aggregation result. So when I set size=0 like below, it gives error-later learned this is for how many results I want(aggregations). So, how can I achieve this to get only the aggregation results(no hits result docs)

AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
                .terms(&quot;my_aggs&quot;)
                .field(&quot;my_field&quot;)
                .size(0) //how to set size for my purpose?
                .order(BucketOrder.key(true));

Moreover, If I get thousands of aggregation results, does this query return all of them? or apply to its default 10 size? If not, how do know how many should I set size of aggregation result.<br><br><br>
Edit I am adding my aggregation like this:

 SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withIndices(indexName)
                .withTypes(typeName)
                .withQuery(boolQueryBuilder)
                .addAggregation(aggregationBuilder)
                .build();

Please help.

答案1

得分: 2

你需要在 SearchRequest 实例上进行操作,而不是在聚合级别进行操作:

AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
        .terms("my_aggs")
        .field("my_field")
        .order(BucketOrder.key(true));

SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withIndices(indexName)
        .withTypes(typeName)
        .withQuery(boolQueryBuilder)
        .withPageable(new PageRequest(0, 1))       // &lt;--- 在此添加
        .addAggregation(aggregationBuilder)
        .build();

据我所知,Spring Data ES 不允许您创建一个大小为 0 的 PageRequest(这就是我选择 1 的原因)。如果这是个问题,这个回答 展示了如何覆盖这个行为。

默认情况下,您的聚合将返回 10 个桶,但如果需要的话,您可以将大小增加到 10000。

英文:

You do that on the SearchRequest instance not at the aggregation level:

AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
            .terms(&quot;my_aggs&quot;)
            .field(&quot;my_field&quot;)
            .order(BucketOrder.key(true));

SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withIndices(indexName)
            .withTypes(typeName)
            .withQuery(boolQueryBuilder)
            .withPageable(new PageRequest(0, 1))       &lt;--- add this
            .addAggregation(aggregationBuilder)
            .build();

As far as I know, Spring Data ES doesn't allow you to create a PageRequest with size 0 (hence why I picked 1). If that's a problem, this answer shows you how to override that behavior.

By default, your aggregation will return 10 buckets, but you can increase the size up to 10000, if needed.

huangapple
  • 本文由 发表于 2020年8月25日 20:27:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63578857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定