# Example of dot\_product similarity on dense\_vector field index document

**URL:** <https://discuss.elastic.co/t/example-of-dot-product-similarity-on-dense-vector-field-index-document/332396>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [May 3, 2023, 9:45am UTC](https://discuss.elastic.co/t/example-of-dot-product-similarity-on-dense-vector-field-index-document/332396 "2023-05-03T09:45:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrian-arapiles](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adrian-arapiles/32/101173_2.png) [@adrian-arapiles](https://discuss.elastic.co/u/adrian-arapiles)\
**Post date:** [May 3, 2023, 9:45am UTC](https://discuss.elastic.co/t/example-of-dot-product-similarity-on-dense-vector-field-index-document/332396/1 "2023-05-03T09:45:05Z")

</div>

Hi,

I'm trying to benchmark different possibilities with dense\_vector and knn search.

I want to test **cosine** similarity vs **dot\_produce** similarity because in documentation says that **dot\_product** is a optimized way to perform cosine similarity.

When I try to index a document with **dot\_product** a faced an error that I'm not sure the meaning of.

> The [dot\_product] similarity can only be used with unit-length vectors. Preview of invalid vector:

In the documentation of **dot\_product** mention something about unit-length but I'm not sure about what it about. I didn't find any example about indexing documents with **dot\_product** similarity.

My example is:

```auto
PUT test
{
  "settings": {
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 2,
        "index": true,
        "similarity": "dot_product",
        "index_options": {
          "type": "hnsw",
          "m": 16,
          "ef_construction": 100
        }
      }
    }
  }
}

PUT test/_doc/1
{
  "vector": [1.0, 2.0] # I tested with [1.0, 2.0] or [1, 2]
}

```

And response:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "The [dot_product] similarity can only be used with unit-length vectors. Preview of invalid vector: [1.0, 2.0]"
    }
  },
  "status": 400
}

```

This example with cosine similarity works as expected.

Could you give me any info about that?

Thanks in advance.

Adrian.

---

<div class="post-metadata">

**Author:** ![adrian-arapiles](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adrian-arapiles/32/101173_2.png) [@adrian-arapiles](https://discuss.elastic.co/u/adrian-arapiles)\
**Post date:** [May 4, 2023, 7:43am UTC](https://discuss.elastic.co/t/example-of-dot-product-similarity-on-dense-vector-field-index-document/332396/2 "2023-05-04T07:43:13Z")

</div>

I think I already found the solution.  
We need to normalize the vector before to index.

I used this code to achieve that:

```auto
public static double[] normalizeVector(double[] vector) {
    double[] normalizedVector = new double[vector.length];
    double magnitude = 0.0;
    for (int i = 0; i < vector.length; i++) {
        magnitude += Math.pow(vector[i], 2);
    }
    magnitude = Math.sqrt(magnitude);
    for (int i = 0; i < vector.length; i++) {
        normalizedVector[i] = vector[i] / magnitude;
    }
    return normalizedVector;
}

```

And now the vector `[1.0, 2.0]` give this results after normalize `[0.4472136, 0.89442719]`.

So this index request works fine.

```auto
PUT test/_doc/1
{
  "vector": [0.4472136, 0.89442719]
}

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 8, 2023, 3:22am UTC](https://discuss.elastic.co/t/example-of-dot-product-similarity-on-dense-vector-field-index-document/332396/3 "2023-05-08T03:22:32Z")

</div>

Thanks heaps for sharing your solution!

---

<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:** [June 5, 2023, 3:23am UTC](https://discuss.elastic.co/t/example-of-dot-product-similarity-on-dense-vector-field-index-document/332396/4 "2023-06-05T03:23:04Z")

</div>

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