How to paging aggregation result in ElasticSearch?

2019-08-10 02:15发布

问题:

When I execute the query below, how to paging the aggs results?

And is there a method to put the aggs results to hits part in json result?

POST  http://myElastic.com/test/e1,e2,e3/_search
{
  "aggs":{
    "dedup" : {
      "terms":{
        "field": "id"
       },
       "aggs":{
         "dedup_docs":{
           "top_hits":{
             "size":1
           }
         }
       }    
    }
  }
}

回答1:

Based on the below issue on the Elasticsearch github site I don't think what you are asking for is possible:

https://github.com/elastic/elasticsearch/issues/4915

Seems like a common request however. Add your own feedback and they may get around to adding it.



回答2:

I searched a moment before found and I came across several positions during my research, so I post a new answer for people who will make the same journey as me.

We can partition the results as below:

{
  "aggs":{
    "group" : {
      "terms":{
        "field": "id",
        "size":5000,
        "include": {
          "partition": 1,
          "num_partitions": 1000
        }
       },
       "aggs":{
         "dedup_docs":{
           "top_hits":{
             "size":1
           }
         }
       }    
    }
  }
}

// size:5000 : return 5.000 results per page
// num_partitions:1000 : return 1.000 pages of results
// partition:1 : return page index 1 (start at 0)

// size:5000,num_partitions:1000,partition:1 : returns results from 5.000 to 9.999
// size:5000,num_partitions:1000,partition:2 : returns results from 10.000 to 14.999
// size:5000,num_partitions:1000,partition:3 : returns results from 15.000 to 19.999