# Averaging dense\_vectors within ES

**URL:** <https://discuss.elastic.co/t/averaging-dense-vectors-within-es/255637>\
**Category:** Elasticsearch\
**Created:** [November 17, 2020, 8:27am UTC](https://discuss.elastic.co/t/averaging-dense-vectors-within-es/255637 "2020-11-17T08:27:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fpug](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fpug/32/79135_2.png) [@fpug](https://discuss.elastic.co/u/fpug)\
**Post date:** [November 17, 2020, 8:27am UTC](https://discuss.elastic.co/t/averaging-dense-vectors-within-es/255637/1 "2020-11-17T08:27:20Z")

</div>

Hi!

I'm designing a clustering algorithm, which leverages ES as a backend to retrieve close matches, using the recently introduced dense\_vector type and the cosineSimilarity function. So far so good, aside from the fact that some approximate-NN techniques baked-in would improve performance.

But let's get to the point: at some point I need to calculate the centroids of the resulting clusters, by averaging the dense vectors of the documents belonging to each cluster. It can be done outside ES of course, but that involves a non-negligible amount of I/O.

So my question is: can I leverage any scripting functionality within ES to efficiently calculate the average of dense\_vectors? Maybe grouping the documents by their cluster\_id with an aggregation?

Thanks in advance, and have a great day 🙂

---

<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:** [December 15, 2020, 8:27am UTC](https://discuss.elastic.co/t/averaging-dense-vectors-within-es/255637/2 "2020-12-15T08:27:22Z")

</div>

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