Libor
Libor

Reputation: 99

Elasticsearch distinct count on nested fields

According to docs, distinct count can be achieved approximately by using cardinality. https://www.elastic.co/guide/en/elasticsearch/guide/current/cardinality.html

I have a large store of data of type like this:

{
    {
        "foo": {
            "bar": "a1"
        }
    },
    {
        "foo": {
            "bar": "a2"
        }
    }
}

and I want to do a distinct count of "foo.bar" values.

My DSL query:

{
    "size": 0,
    "aggs": {
        "number_of_bars": {
            "cardinality": {
                "field": "bar"
            }
        }
    }
}

returns "number_of_bars": 0. I was also trying "field": "foo.bar", which results in an error.

Can you tell me, what I am doing wrong?

Upvotes: 0

Views: 962

Answers (1)

TechnocratSid
TechnocratSid

Reputation: 2415

Use this:

{
    "size": 0,
    "aggs": {
        "number_of_bars": {
            "cardinality": {
                "field": "foo.bar.keyword"
            }
        }
    }
}

Upvotes: 2

Related Questions