# Searching with Dense Vector

**URL:** <https://discuss.elastic.co/t/searching-with-dense-vector/213374>\
**Category:** Elasticsearch\
**Created:** [December 30, 2019, 6:14pm UTC](https://discuss.elastic.co/t/searching-with-dense-vector/213374 "2019-12-30T18:14:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dipanjan\_Nag](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@Dipanjan\_Nag](https://discuss.elastic.co/u/Dipanjan_Nag)\
**Post date:** [December 30, 2019, 6:14pm UTC](https://discuss.elastic.co/t/searching-with-dense-vector/213374/1 "2019-12-30T18:14:59Z")

</div>

Is there a way to apply a query directly to [DenseVector](https://www.elastic.co/guide/en/elasticsearch/reference/current/dense-vector.html) e.g. Nearest Neighbors. I understand that a vector field (i.e. Dense or Sparse) is accessible via [scoring](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-score-query.html#vector-functions). But I'm rather curious about true sense vector search like provided via [FAISS](https://github.com/facebookresearch/faiss) i.e. is it possible to send query like find me n number of nearest neighbors to a certain vector of same dimension?

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [January 2, 2020, 9:34pm UTC](https://discuss.elastic.co/t/searching-with-dense-vector/213374/2 "2020-01-02T21:34:02Z")

</div>

Hello,  
the way to find `n` number of nearest neighbors is through scoring.  
Scoring functions let you define what is "nearest" to you.

For example, the following query finds top 5 nearest documents, where nearest is defined as inversely proportional to euclidean distance.

```auto
GET my_index/_search
{
  "size" : 5,
  "query": {
    "script_score": {
      "query" : {
        "match_all" : {}
      },
      "script": {
        "source": "1 / (1 + l2norm(params.queryVector, doc['my_dense_vector']))",
        "params": {
          "queryVector": [4, 3.4, -0.2]
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Dipanjan\_Nag](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@Dipanjan\_Nag](https://discuss.elastic.co/u/Dipanjan_Nag)\
**Post date:** [January 3, 2020, 9:26am UTC](https://discuss.elastic.co/t/searching-with-dense-vector/213374/3 "2020-01-03T09:26:40Z")

</div>

Thanks a lot for the response. However the `score` will be calculated for all `doc['my_dense_vector']` in this query, so performance might be hit. Am I correct assuming that?

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [January 3, 2020, 5:45pm UTC](https://discuss.elastic.co/t/searching-with-dense-vector/213374/4 "2020-01-03T17:45:10Z")

</div>

Correct, `score` will be calculated by all documents matching the `query`. You can provide a more restrictive query to limit the number of matching documents.

And you are right, calculating `score` for all documents can be slow. We are working on ways to do approximate knn search.

---

<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:** [January 31, 2020, 5:45pm UTC](https://discuss.elastic.co/t/searching-with-dense-vector/213374/5 "2020-01-31T17:45:15Z")

</div>

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