在Elasticsearch中仅获取聚合结果

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

Getting only aggregations results in elasticsearch

问题

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

  1. {
  2. "size": 0,
  3. "aggs": {
  4. "my_aggs": {
  5. "terms": {
  6. "field": "my_field"
  7. }
  8. }
  9. }
  10. }

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

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

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

  1. SearchQuery searchQuery = new NativeSearchQueryBuilder()
  2. .withIndices(indexName)
  3. .withTypes(typeName)
  4. .withQuery(boolQueryBuilder)
  5. .addAggregation(aggregationBuilder)
  6. .build();

请提供帮助。

英文:

I am doing aggregation like this:

  1. {
  2. &quot;size&quot;:0,
  3. &quot;aggs&quot;:
  4. {
  5. &quot;my_aggs&quot;:
  6. {
  7. &quot;terms&quot;:
  8. {
  9. &quot;field&quot;:&quot;my_field&quot;
  10. }
  11. }
  12. }
  13. }

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)

  1. AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
  2. .terms(&quot;my_aggs&quot;)
  3. .field(&quot;my_field&quot;)
  4. .size(0) //how to set size for my purpose?
  5. .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:

  1. SearchQuery searchQuery = new NativeSearchQueryBuilder()
  2. .withIndices(indexName)
  3. .withTypes(typeName)
  4. .withQuery(boolQueryBuilder)
  5. .addAggregation(aggregationBuilder)
  6. .build();

Please help.

答案1

得分: 2

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

  1. AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
  2. .terms("my_aggs")
  3. .field("my_field")
  4. .order(BucketOrder.key(true));
  5. SearchQuery searchQuery = new NativeSearchQueryBuilder()
  6. .withIndices(indexName)
  7. .withTypes(typeName)
  8. .withQuery(boolQueryBuilder)
  9. .withPageable(new PageRequest(0, 1)) // &lt;--- 在此添加
  10. .addAggregation(aggregationBuilder)
  11. .build();

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

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

英文:

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

  1. AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
  2. .terms(&quot;my_aggs&quot;)
  3. .field(&quot;my_field&quot;)
  4. .order(BucketOrder.key(true));
  5. SearchQuery searchQuery = new NativeSearchQueryBuilder()
  6. .withIndices(indexName)
  7. .withTypes(typeName)
  8. .withQuery(boolQueryBuilder)
  9. .withPageable(new PageRequest(0, 1)) &lt;--- add this
  10. .addAggregation(aggregationBuilder)
  11. .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:

确定