Filter out all except nearest item per group based on location

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:

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.
To view this discussion on the web visit 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.