# Aggregate into "best fit" buckets?

**URL:** <https://discuss.elastic.co/t/aggregate-into-best-fit-buckets/195822>\
**Category:** Elasticsearch\
**Created:** [August 19, 2019, 11:44pm UTC](https://discuss.elastic.co/t/aggregate-into-best-fit-buckets/195822 "2019-08-19T23:44:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickovs](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@nickovs](https://discuss.elastic.co/u/nickovs)\
**Post date:** [August 19, 2019, 11:44pm UTC](https://discuss.elastic.co/t/aggregate-into-best-fit-buckets/195822/1 "2019-08-19T23:44:23Z")

</div>

I'm looking for a way to put my data into one of a set of buckets depending on to which of a list of labels is closest to some field in each document. By way of example, say I have a geo-location point in each document and I have a list of the lat/lon points for a set of cities and then I'd like to put my documents into buckets depending on which city is closest.

In my current case the labels on the buckets are geo-locations but I have some other cases where I'd like to group the documents based on which of a set of text values lies closest.

Is there currently any mechanism in Elasticsearch to compute a set of scores for each document against a set of target values and then place the documents into buckets based on which target value gives the highest (or lowest) score? It seems like it would be a generically useful feature for a whole host of purposes; it even lets you compute a round of K-means clustering in a single aggregation query without the data leaving the servers.

Any suggestions for how to do this would be much appreciated.

---

<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:** [September 16, 2019, 11:44pm UTC](https://discuss.elastic.co/t/aggregate-into-best-fit-buckets/195822/2 "2019-09-16T23:44:27Z")

</div>

Closed on September 16, 2019, 11:44pm UTC

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