# Return unique result by field values

**URL:** <https://discuss.elastic.co/t/return-unique-result-by-field-values/124441>\
**Category:** Elasticsearch\
**Created:** [March 18, 2018, 11:05am UTC](https://discuss.elastic.co/t/return-unique-result-by-field-values/124441 "2018-03-18T11:05:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![woshitom](https://avatars.discourse-cdn.com/v4/letter/w/a587f6/32.png) [@woshitom](https://discuss.elastic.co/u/woshitom)\
**Post date:** [March 18, 2018, 11:05am UTC](https://discuss.elastic.co/t/return-unique-result-by-field-values/124441/1 "2018-03-18T11:05:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 18, 2018, 12:43pm UTC](https://discuss.elastic.co/t/return-unique-result-by-field-values/124441/2 "2018-03-18T12:43:32Z")

</div>

A terms agg in type and top hits then?

Might work but never tested it.

---

<div class="post-metadata">

**Author:** ![woshitom](https://avatars.discourse-cdn.com/v4/letter/w/a587f6/32.png) [@woshitom](https://discuss.elastic.co/u/woshitom)\
**Post date:** [March 18, 2018, 12:56pm UTC](https://discuss.elastic.co/t/return-unique-result-by-field-values/124441/3 "2018-03-18T12:56:18Z")

</div>

But will the top hits will have distinct types ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 18, 2018, 1:47pm UTC](https://discuss.elastic.co/t/return-unique-result-by-field-values/124441/4 "2018-03-18T13:47:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 15, 2018, 1:47pm UTC](https://discuss.elastic.co/t/return-unique-result-by-field-values/124441/5 "2018-04-15T13:47:13Z")

</div>

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