Return unique result by field values

I have 3 "places" having each a type and a location:

PUT places/1
{
   "type" : "A",
   "location": {
      "lat": 1.378446,
      "lon": 103.763427
   }
}

PUT places/2
{
   "type" : "B",
   "location": {
      "lat": 1.478446,
      "lon": 104.763427
   }
}

PUT places/3
{
   "type" : "A",
   "location": {
      "lat": 1.278446,
      "lon": 102.763427
   }
}

I'd like to retrieve only one (the closest from a given location) place per "type", so in my example result answer should be composed by exactly 2 elements (one for "type: A" and one for "type: B").

I'd also prefer to avoid "aggregations" as I will need the _source of each places.

Any help would be great.

A terms agg in type and top hits then?

Might work but never tested it.

But will the top hits will have distinct types ?

As you are first breaking by type with the terms agg: yes.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.