# Mapping issue with for KNN and Cosine Similarity

**URL:** <https://discuss.elastic.co/t/mapping-issue-with-for-knn-and-cosine-similarity/354360>\
**Category:** Elastic Search\
**Tags:** painless, elastic-site-search\
**Created:** [February 28, 2024, 3:07pm UTC](https://discuss.elastic.co/t/mapping-issue-with-for-knn-and-cosine-similarity/354360 "2024-02-28T15:07:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![latif](https://avatars.discourse-cdn.com/v4/letter/l/e480ec/32.png) [@latif](https://discuss.elastic.co/u/latif)\
**Post date:** [February 28, 2024, 3:07pm UTC](https://discuss.elastic.co/t/mapping-issue-with-for-knn-and-cosine-similarity/354360/1 "2024-02-28T15:07:36Z")

</div>

Hi,  
I am trying to create a mapping which will take image embeddings as input and should store in a field which will allow me to apply cosine or knn upon my request. I am trying to map a field with knn and coosine property in both.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [February 28, 2024, 3:21pm UTC](https://discuss.elastic.co/t/mapping-issue-with-for-knn-and-cosine-similarity/354360/2 "2024-02-28T15:21:25Z")

</div>

You want a [dense\_vector](https://www.elastic.co/guide/en/elasticsearch/reference/current/dense-vector.html) field type but you have to choose the similarity e.g. cosine when the mapping is created.

---

<div class="post-metadata">

**Author:** ![rtwolfe94022](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rtwolfe94022/32/132253_2.png) [@rtwolfe94022](https://discuss.elastic.co/u/rtwolfe94022)\
**Post date:** [March 3, 2024, 1:57pm UTC](https://discuss.elastic.co/t/mapping-issue-with-for-knn-and-cosine-similarity/354360/3 "2024-03-03T13:57:31Z")

</div>

@Kathleen_DeRusso is correct

To store image embeddings and perform cosine similarity or k-nearest neighbor (KNN) searches in Elasticsearch, use a `dense_vector` field in your mapping. This field type allows storing embeddings and supports cosine similarity out of the box. For KNN, you might need additional plugins, depending on your Elasticsearch version. Here's a quick guide:

1. **Create an index** with a `dense_vector` field for your embeddings, specifying the dimension size.
2. **Index your image embeddings** as dense vectors.
3. **For cosine similarity** , use a `script_score` query with a cosine similarity calculation to find similar images.
4. **For KNN searches** , your approach might vary based on Elasticsearch version and available plugins.

Remember, script-based similarity calculations can impact performance, so monitor and optimize your setup as needed.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [March 3, 2024, 4:07pm UTC](https://discuss.elastic.co/t/mapping-issue-with-for-knn-and-cosine-similarity/354360/4 "2024-03-03T16:07:22Z")

</div>

@latif Welcome to the community.

---

<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:** [March 31, 2024, 4:07pm UTC](https://discuss.elastic.co/t/mapping-issue-with-for-knn-and-cosine-similarity/354360/5 "2024-03-31T16:07:40Z")

</div>

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