# Named query in hybrid queries

**URL:** <https://discuss.elastic.co/t/named-query-in-hybrid-queries/336608>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [June 21, 2023, 4:16pm UTC](https://discuss.elastic.co/t/named-query-in-hybrid-queries/336608 "2023-06-21T16:16:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Nazari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali_nazari/32/122558_2.png) [@Ali\_Nazari](https://discuss.elastic.co/u/Ali_Nazari)\
**Post date:** [June 21, 2023, 4:16pm UTC](https://discuss.elastic.co/t/named-query-in-hybrid-queries/336608/1 "2023-06-21T16:16:32Z")

</div>

How can I recognize which of my elasticsearch hits are because of my KNN and which are related to the query part? I don't know where to use a named query in **KNN** part.

```auto
GET post-vector/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "data.bag_type": {
              "query": "a",
              "_name": "first"
            }
          }
        },
        {
          "match": {
            "data.belt_type": {
              "query": "b",
              "_name": "last"
            }
          }
        }
      ]
    }
  },
  "knn": {
    "field": "vector_data.text_vector",
    "k": 5,
    "query_vector": [
      0.012099707499146461,
      0.07153409719467163,
      0.11289671808481216,
      0.02117152325809002,
      -0.0389116108417511,
      0.14162440598011017
    ],
    "num_candidates": 100
  }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 21, 2023, 6:41pm UTC](https://discuss.elastic.co/t/named-query-in-hybrid-queries/336608/2 "2023-06-21T18:41:24Z")

</div>

Hi @Ali_Nazari

According to the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#_combine_approximate_knn_with_other_features) you have a combination.

> The `knn` and `query` matches are combined through a disjunction, as if you took a boolean _or_ between them. The top `k` vector results represent the global nearest neighbors across all index shards.
> 
> The score of each hit is the sum of the `knn` and `query` scores. You can specify a `boost` value to give a weight to each score in the sum. In the example above, the scores will be calculated as
> 
> score = 0.9 \* match\_score + 0.1 \* knn\_score
> 
> The `knn` option can also be used with [`aggregations`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html). In general, Elasticsearch computes aggregations over all documents that match the search. So for approximate kNN search, aggregations are calculated on the top `k` nearest documents. If the search also includes a `query`, then aggregations are calculated on the combined set of `knn` and `query` matches.

---

<div class="post-metadata">

**Author:** ![Ali\_Nazari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali_nazari/32/122558_2.png) [@Ali\_Nazari](https://discuss.elastic.co/u/Ali_Nazari)\
**Post date:** [June 22, 2023, 8:35am UTC](https://discuss.elastic.co/t/named-query-in-hybrid-queries/336608/3 "2023-06-22T08:35:39Z")

</div>

Hi @RabBit_BR

To be precise, I want to distinguish which result is exactly related to `KNN`, and which matched with the `query` part to analyze my vectorizer.

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [June 22, 2023, 11:27am UTC](https://discuss.elastic.co/t/named-query-in-hybrid-queries/336608/4 "2023-06-22T11:27:56Z")

</div>

@Ali_Nazari , you can run the search with `explain: true` and it will give a detailed account of the scoring for each returned document.

---

<div class="post-metadata">

**Author:** ![Ali\_Nazari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali_nazari/32/122558_2.png) [@Ali\_Nazari](https://discuss.elastic.co/u/Ali_Nazari)\
**Post date:** [June 24, 2023, 10:54am UTC](https://discuss.elastic.co/t/named-query-in-hybrid-queries/336608/5 "2023-06-24T10:54:06Z")

</div>

@BenTrent Thanks a lot.

---

<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:** [July 22, 2023, 10:54am UTC](https://discuss.elastic.co/t/named-query-in-hybrid-queries/336608/6 "2023-07-22T10:54:23Z")

</div>

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