Vista
Vista

Reputation: 93

Why am I getting number of buckets always equal to the specified size in terms aggregations?

I am a newbie in elastic search, I am using terms aggregation to get only the unique documents based on a field from the index. I have specified maximum size of unique documents in my query, why the bucket count is always equal to size?

{
  "aggs": {
    "name": {
      "terms": {
        "field": "fieldname",
        "size": 10000
      }
    }
  }
}

why am I getting 10000 buckets, when unique documents may be less than that?

Upvotes: 0

Views: 218

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9099

10000 is the upper cap for the number of documents returned in a query. Your index will be having more than 10000 records. To get actual count use value count api

GET index/_count

OR

{
  "size": 0, 
  "aggs": {
    "total_doc_count": {
      "value_count": {
        "field": "fieldname"
      }
    }
  }
}

To fetch more than 10000 documents in a query , you have to use scroll api.

POST /index-name/_search?scroll=1m --> scroll context
{
    "size": 10000,  --> will return docs in chunk of 10,000
    "query": {
        "match_all": {}
    }
}

POST /_search/scroll 
{
    "scroll" : "1m", 
    "scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==" --> you will get from previous request
}

If there are only 100 documents, terms aggregation will return only 100 not 10000

Upvotes: 0

Related Questions