dKab
dKab

Reputation: 2858

How to get total number of aggregation buckets in Elasticsearch?

I use Elasticsearch terms aggregation to see how many documents have a certain value in their "foo" field like this:

{
    ...
    "aggregations": {
        "metastore": { 
            "terms": { 
                "field": "foo",
                "size": 50 
            }
        }
    }
}

and I get the response:

 "aggregations": {
      "foo": {
            "buckets": [
                {
                    "key_as_string": "2018-10-01T00:00:00.000Z",
                    "key": 1538352000000,
                    "doc_count": 935
                },
                {
                    "key_as_string": "2018-11-01T00:00:00.000Z",
                    "key": 1541030400000,
                    "doc_count": 15839
                },
                ...
                /* 48 more values */
             ]
      }
 }

But I'm limiting the number of different values to 50. If there are more different values in this field they won't be returned in the response, and that's fine, because I don't need to all of them, but I would like to know how many of them there are. So, how could I get the total number of different values? It would be fantastic if the answer provided a full example query, thanks.

Upvotes: 2

Views: 2252

Answers (2)

Nishant
Nishant

Reputation: 7874

You can probably add a cardinality aggregation which will give you unique number of terms for the field. This will be equal to the number of buckets for the terms aggregation.

{
    ...
    "aggregations": {
        "metastore": { 
            "terms": { 
                "field": "foo",
                "size": 50 
            }
        },
        "uniquefoo": {
          "cardinality": {
            "field": "foo"
          }
        }
    }
}

NOTE: Please keep in mind that cardinality aggregation might in some cases return approx count. To know more on it read here.

Upvotes: 3

Val
Val

Reputation: 217544

The cardinality aggregation is there to help. Just note, however, that the number that is returned is an approximation and might not reflect the exact number of buckets you'd get if you were to request them all. However, the accuracy is pretty good on low cardinality fields.

{
    ...
    "aggregations": {
        "unique_count": {
          "cardinality": {
            "field": "foo"
          }
        },
        "metastore": { 
            "terms": { 
                "field": "foo",
                "size": 50 
            }
        }
    }
}

Upvotes: 2

Related Questions