'Elasticsearch KNN search with keyword terms

I am currently trying to build a search where the knn search will be used within the set of results from a terms query. Below is my test query, but I run into an error "malformed query, expected [END_OBJECT] but found [FEILD_NAME]"

GET my-index2/_search
{
  "query":{
    "bool":{
      "should":[
        {"terms":{"t":["hello"]}}
        ]
    },
    "knn": {
      "my_vector2": {
        "vector": [2, 3, 5, 6],
        "k": 2
      }
    }
  },
  "_source":["t"]
}

Is there anyway to accomplish this?



Solution 1:[1]

From elasticsearch 8.2, you can directly use filters with _knn_search:

GET my-index/_knn_search
{
  "knn": {
    "field": "image_vector",
    "query_vector": [0.3, 0.1, 1.2],
    "k": 5,
    "num_candidates": 50
  },
  "filter": {
    "term": {
      "file_type": "svg"
    }
  },
  "_source": ["name"]
}

The example is from https://www.elastic.co/guide/en/elasticsearch/reference/8.2/knn-search-api.html.

Solution 2:[2]

I think your query is malformed and should be like below, i.e. the knn query should be inside the bool/filter array so that it works on the same set of data as the one selected by the terms query.:

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "t": [
              "hello"
            ]
          }
        },
        {
          "knn": {
            "my_vector2": {
              "vector": [
                2,
                3,
                5,
                6
              ],
              "k": 2
            }
          }
        }
      ]
    }
  },
  "_source": [
    "t"
  ]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sung Kim
Solution 2 Val