# Filter out all except nearest item per group based on location

**URL:** <https://discuss.elastic.co/t/filter-out-all-except-nearest-item-per-group-based-on-location/16114>\
**Category:** Elasticsearch\
**Created:** [March 3, 2014, 6:14pm UTC](https://discuss.elastic.co/t/filter-out-all-except-nearest-item-per-group-based-on-location/16114 "2014-03-03T18:14:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johannes\_Bornhold](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Johannes\_Bornhold](https://discuss.elastic.co/u/Johannes_Bornhold)\
**Post date:** [March 3, 2014, 6:14pm UTC](https://discuss.elastic.co/t/filter-out-all-except-nearest-item-per-group-based-on-location/16114/1 "2014-03-03T18:14:49Z")

</div>

Hi,

I am running my first experiments with elasticsearch and trying to figure  
out how to address the following problem:

I have items in my index and every item has a location. It can be that some  
items belong to the same group, in this case I want to only find the  
nearest item per group.

With some background from SQL databases, I thought that I will need to  
aggregate some data and started adding aggregations around my items: First  
terms based on the field "group" and then a "min" aggregation based on a  
script which computes the distance to a given location. That gave me great  
results, but I did not manage to use them to filter out items that I did  
not want to find in the result anymore. As far as I understand, this is  
probably not the right approach to solve this problem.

Second attempt was to include some more data in the item document itself,  
so that each item contains also the locations of all other items of the  
same group. This way I can already figure out if this item is the nearest  
one with a script and use that in a filter to only keep the nearest one in  
the result.

Since I am a bit new to the world of elasticsearch, I am wondering if I  
should continue the second approach (it solves at least the problem) or if  
there is further options for this problem in elasticsearch, which I might  
not yet have discovered.

I added a gist with the curl commands I am currently using:

> <https://gist.github.com/johbo/9328423>

Cheers, Johannes

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a0a781d5-e529-4595-91cf-db2f096008e2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a0a781d5-e529-4595-91cf-db2f096008e2%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 1:46am UTC](https://discuss.elastic.co/t/filter-out-all-except-nearest-item-per-group-based-on-location/16114/2 "2017-07-06T01:46:08Z")

</div>

Closed on July 6, 2017, 1:46am UTC


