# How do I get k nearest neighbors from a numerical vector based index?

**URL:** <https://discuss.elastic.co/t/how-do-i-get-k-nearest-neighbors-from-a-numerical-vector-based-index/214110>\
**Category:** Elasticsearch\
**Created:** [January 7, 2020, 6:36pm UTC](https://discuss.elastic.co/t/how-do-i-get-k-nearest-neighbors-from-a-numerical-vector-based-index/214110 "2020-01-07T18:36:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [January 7, 2020, 6:36pm UTC](https://discuss.elastic.co/t/how-do-i-get-k-nearest-neighbors-from-a-numerical-vector-based-index/214110/1 "2020-01-07T18:36:41Z")

</div>

Say I indexed something like this:

```
{
{'l1':[1,2,3,4],'l2':[5,6,7,7]}
{'l1':[1,2,6,6],'l2':[4,3,2,7]}
...
..
{'l1':[1,2,3,24],'l2':[5,6,37,1]}

}

```

And I have a dictionary as {'l1':[2,3,4,5]}

and I want to find k values of 'l1' which are nearest in the vector space to it, how do I go about this? Is this possible to do in elasticsearch?

I'm using the python API

---

<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:** [January 8, 2020, 10:22am UTC](https://discuss.elastic.co/t/how-do-i-get-k-nearest-neighbors-from-a-numerical-vector-based-index/214110/2 "2020-01-08T10:22:45Z")

</div>

you may want to read about the new [vector datatype](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/dense-vector.html) that has been recently added.

In addition it might help, if your clear up your question a bit? Are the above separate documents? Is `2,3,4,5` your query vector?

A fully fledged example with sample documents might help to understand the setting a bit more.

---

<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:** [February 5, 2020, 10:27am UTC](https://discuss.elastic.co/t/how-do-i-get-k-nearest-neighbors-from-a-numerical-vector-based-index/214110/3 "2020-02-05T10:27:37Z")

</div>

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