Named query in hybrid queries

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.

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
  }
}

Hi @Ali_Nazari

According to the documentation 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. 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.

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.

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

2 Likes

@BenTrent Thanks a lot.

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