# Results with unique value in field?

**URL:** https://discuss.elastic.co/t/results-with-unique-value-in-field/78422
**Category:** Elasticsearch
**Created:** [March 13, 2017, 10:11pm UTC](https://discuss.elastic.co/t/results-with-unique-value-in-field/78422 "2017-03-13T22:11:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jaddison](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@jaddison](https://discuss.elastic.co/u/jaddison)
#### Post date: [March 13, 2017, 10:11pm UTC](https://discuss.elastic.co/t/results-with-unique-value-in-field/78422/1 "2017-03-13T22:11:56Z")

</div>

Using ES 5.2. I've indexed activities (classes, local events, etc) that are put on by businesses. Along these lines:

```auto
{
  "name": "Johnny's BBQ Fundraiser",
  "price": "$15/plate",
  "date": "2016-04-28 12:00:00",
  "location": "City Hall",
  "business": {
    "id": 423,
    "name": "Johnny's Smokey BBQ Restaurant"
  }
}

```

There are many different businesses, and each business runs (owns) many different activities.

I want to query for results such that each result is from a unique `business.id`. How would I go about doing this?

Currently I'm inefficiently looping:

- query ES for 30 results (I'm only looking for 16)
- taking the first activity for each encountered `business.id`, temporarily store the encountered `business.id` in a list, skipping other activities in the loop with already encountered `business.id`s
- on the subsequent loops, I query for another 30 results excluding (via `bool` : `must_not` : `terms` filter) previously encountered `business.id`s from the list

This is terrible. The way our data is structured and how businesses interact with our site means that there are sometimes 5 or 6 loops, just to get 16 results unique by `business.id`.

There must be a better way.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [March 13, 2017, 10:52pm UTC](https://discuss.elastic.co/t/results-with-unique-value-in-field/78422/2 "2017-03-13T22:52:46Z")

</div>

Hey,

not a hundred percent sure, if this is what you want, but I think the next minor version release (Elasticsearch 5.3) has exactly, what you are looking for. The feature is called field collapsing for search, see the docs

[https://www.elastic.co/guide/en/elasticsearch/reference/5.x/search-request-collapse.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/search-request-collapse.html)

--Alex

---

<div class="post-metadata">

### Author: ![jaddison](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@jaddison](https://discuss.elastic.co/u/jaddison)
#### Post date: [March 13, 2017, 11:08pm UTC](https://discuss.elastic.co/t/results-with-unique-value-in-field/78422/3 "2017-03-13T23:08:33Z")

</div>

That is interesting - thanks Alexander.

I've been looking into Aggregations - specifically if I bucket by `business.id` and then have a top-hit agg with size=1... might this work for my scenario? It does mean I need to parse my data from aggregations rather than just 'normal' results (which is what I'd obviously prefer - and looks like `collapse` does).

**Edit** : In fact, I'd almost guess that is what `collapse` does, internally... if so, intriguing!

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [March 13, 2017, 11:26pm UTC](https://discuss.elastic.co/t/results-with-unique-value-in-field/78422/4 "2017-03-13T23:26:17Z")

</div>

Hey,

yes, that could be a valid workaround for now, but the new feature should be faster than aggs, if I recall correctly.

--Alex

---

<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 10, 2017, 11:26pm UTC](https://discuss.elastic.co/t/results-with-unique-value-in-field/78422/5 "2017-04-10T23:26:34Z")

</div>

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