1

I want to perform following in the elasticsearch.

select distinct name, city from students where class = 10 and age = 15;

and it should return all distinct values for name and city. How to perform this?

stackmj18
  • 103
  • 2
  • 12

1 Answers1

1

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-collapse. Field collapsing Should be helpful here.

{
    "query": { 
    "bool": {
     "must": [
       {"match": 
         {
         "class": 10
         }
       },
       {"match": 
         {
         "age": 15
         }
       }
     ] 
    }
  },
    "collapse" : {
        "field" : "name.keyword" ,
        "inner_hits" : {
          "name": "by_type",
            "collapse" : {"field" : "city.keyword"}
        }
    }, 
    "size": 100
}

The above query will return one document par name and city after matching with class and age.

Ani Guner
  • 106
  • 3