2

I am trying to get unique values from elasticsearch using this dsl query

{
  "aggs": {
    "distinct": {
      "terms": {
        "field": "model",
        "size": 0
      }
    }
  }
}

The problem with this is that if I have two models i.e. moto g and htc one, I get four results, moto, g, htc, one. How can I configure it to return two results?

1 Answer 1

2

You need to modify the mapping of your model field to add a not_analyzed sub-field instead and run the aggregation on that sub-field instead, like this:

curl -XPUT localhost:9200/your_index/_mapping/your_type -d '{
  "properties": {
    "model": {
      "type": "string",
      "fields": {                      <-- add this section
        "raw": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}'

Once your mapping is updated, you need to reindex your data and then you'll be able to run your aggregation like this:

{
  "aggs": {
    "distinct": {
      "terms": {
        "field": "model.raw",          <--- use the new sub-field
        "size": 0
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am using logstash to send my logs to elasticsearch. When I used model.raw directly, it threw errors ---> doc_count_error_upper_bound:0 and sum_other_doc_count:0. Does logstash not send raw fields? Or do I have to modify the query in some other format?
You can either modify the elasticsearch output in the Logstash config in order to apply your own template with manage_template => true or use an index template in ES. Whichever way suits you best.
The change in logstash filter didn't work for some unknown reason as no errors were thrown and I didn't want to create a new template in my elasticsearch. I ended up updating the mapping and re-indexing the data. But it worked! Thanks a ton!

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.