# Exactly which documents are used for vector calculation

**URL:** <https://discuss.elastic.co/t/exactly-which-documents-are-used-for-vector-calculation/202270>\
**Category:** Elasticsearch\
**Created:** [October 4, 2019, 2:53am UTC](https://discuss.elastic.co/t/exactly-which-documents-are-used-for-vector-calculation/202270 "2019-10-04T02:53:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ray1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ray1/32/55365_2.png) [@ray1](https://discuss.elastic.co/u/ray1)\
**Post date:** [October 4, 2019, 2:53am UTC](https://discuss.elastic.co/t/exactly-which-documents-are-used-for-vector-calculation/202270/1 "2019-10-04T02:53:29Z")

</div>

I have read some of elastic search blogs and documents to understand how the vector calculation works.  
However it seems confusing for me about the range of documents that is used for the vector calculation.

[https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-script-score-query.html#vector-functions](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-script-score-query.html#vector-functions)

> **[Text similarity search in Elasticsearch using vector fields](https://www.elastic.co/kr/blog/text-similarity-search-with-vectors-in-elasticsearch)**
>
> The Elasticsearch 7.3 release brings support for using vectors in document scoring. This post explores how text embeddings and vector fields can be used to support similarity search.

1. If match\_all option is not used, documents, that are used for the vector calculation, are chosen based on the "text query" calculation?  
Which means the term-based search first identifies a target document from all documents and a vector operation is done against this restricted target document?

2. If match\_all option is used, vector calculation is done for whole documents?

---

<div class="post-metadata">

**Author:** ![Julie\_Tibshirani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/julie_tibshirani/32/55628_2.png) [@Julie\_Tibshirani](https://discuss.elastic.co/u/Julie_Tibshirani)\
**Post date:** [October 9, 2019, 4:43pm UTC](https://discuss.elastic.co/t/exactly-which-documents-are-used-for-vector-calculation/202270/2 "2019-10-09T16:43:50Z")

</div>

Hello ray1, your overall understanding is correct.

Currently, vectors can only be used for scoring through Painless script functions like `cosineSimilarity` and `l2norm`. The way to use these functions for scoring during a search is to use a `script_score` query. The `script_score` query wraps another query and provides a new score for every document it matches.

When the wrapped query is a `match_all` (as in the blog post), then all documents are returned and the vector calculation is performed against each one. However, the query could instead be a more restrictive one like `term`:

```auto
{
  "script_score": {
    "query": {"term": { "tags": "java" }},
    "script": {
      "source": "cosineSimilarity(params.query_vector, doc['title_vector']) + 1.0",
      "params": {"query_vector": query_vector}
    }
  }
}

```

With this query, we only consider documents with a `tags` field that contains `java`. The vector calculation only runs over this subset of documents.

---

<div class="post-metadata">

**Author:** ![ray1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ray1/32/55365_2.png) [@ray1](https://discuss.elastic.co/u/ray1)\
**Post date:** [October 15, 2019, 9:32am UTC](https://discuss.elastic.co/t/exactly-which-documents-are-used-for-vector-calculation/202270/3 "2019-10-15T09:32:31Z")

</div>

Thank you for your relpy

---

<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:** [November 12, 2019, 9:32am UTC](https://discuss.elastic.co/t/exactly-which-documents-are-used-for-vector-calculation/202270/4 "2019-11-12T09:32:47Z")

</div>

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