0

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?

2
  • What was the error ? Cause with field foo.bar it works fine for me. Can you also post the relevant part of the mapping please ? Thx Commented Jul 4, 2018 at 13:04
  • The error was: "Fielddata is disabled on text fields by default. Set fielddata=true on [foo.bar] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead." I don't know what is meant by "relevant part of mapping", sorry I'm new to elasticsearch. Commented Jul 4, 2018 at 13:40

1 Answer 1

2

Use this:

{
    "size": 0,
    "aggs": {
        "number_of_bars": {
            "cardinality": {
                "field": "foo.bar.keyword"
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.