Sanjay
Sanjay

Reputation: 111

Elastic search - Return Unique values based on a filter/match

PUT items/1
{ "language" : 10,
  "country" : "US" }

PUT items/2
{ "language" : 11,
  "country" : "UK" }

PUT items/3
{ "language" : 10,
  "country" : "US" }

PUT items/4
{ "language" : 12,
  "country" : "US" }

How do i write a search query that returns unique languages i.e., 10, 12 whose country is US

For getting a list of all unique languages 10,11,12 terms aggregation can be used. However, i am unable to figure out how to include the country filter into it.

Upvotes: 2

Views: 298

Answers (1)

keety
keety

Reputation: 17441

Based on the use-case one way to achieve this is using filter aggregations : Example:

{
   "size": 0,
   "aggs": {
      "country_us": {
         "filter": {
            "term": {
               "country": "us"
            }
         },
         "aggs": {
            "language": {
               "terms": {
                  "field": "language",
                  "size": 0
               }
            }
         }
      }
   }
}

Upvotes: 2

Related Questions