0

I want to get the unique values from elasticsearch in the field named "name", i do not know how can i put the condition where the values have to be unique.

The purpose of this work is the fetch all the unique names from the elasticsearch database.

So basically what i need is a aggregation query that fetch the unique values

Can someone help me to fix this issue, thanks a lot in advanced.

1 Answer 1

1

You can use a terms aggregation on a field which is not_analyzed.

However, this is by default limited to the 10 most popular terms. You can change this by updating the size parameter of the terms aggregation. Setting it to 0 will allow you to have up to Integer.MAX_VALUE different terms (see the documentation here).

Here is an example mapping:

POST terms
{
  "mappings":{
    "test":{
      "properties":{
        "title":{
          "type":"string",
          "index":"not_analyzed"
        }
      }
    }
  }
}

Adding some documents :

POST terms/test
{
  "title":"Foundation"
}


POST terms/test
{
  "title":"Foundation & Empire"
}

Finally, the request :

POST terms/_search?search_type=count
{
  "aggs": {
    "By Title": {
      "terms": {
        "field": "title",
        "size": 0
      }
    }
  }
}

will give you what you need :

"aggregations": {
      "By Title": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "Foundation",
               "doc_count": 1
            },
            {
               "key": "Foundation & Empire",
               "doc_count": 1
            }
         ]
      }
   }

Be aware that if you have a large number of terms, this request will be very expensive to execute.

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.