Limit ElasticSearch aggregation to top n query results

后端 未结 3 558
粉色の甜心
粉色の甜心 2020-12-09 17:24

I have a set of 2.8 million docs with sets of tags that I\'m querying with ElasticSearch, but many of these docs can be grouped together by one ID. I want to query my data u

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 18:09

    The size parameter can be set to define how many term buckets should be returned out of the overall terms list.

    By default, the node coordinating the search process will request each shard to provide its own top size term buckets and once all shards respond, it will reduce the results to the final list that will then be returned to the client. This means that if the number of unique terms is greater than size, the returned list is slightly off and not accurate (it could be that the term counts are slightly off and it could even be that a term that should have been in the top size buckets was not returned).

    If set to 0, the size will be set to Integer.MAX_VALUE.

    Here is an example code to return top 100:

    {
    "aggs" : {
        "products" : {
            "terms" : {
                "field" : "product",
                "size" : 100
                      }
                     }
             }
    }
    

    You can refer to this for more information.

提交回复
热议问题