Elasticsearch KNN search with keyword terms

Viewed 675

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?

2 Answers

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"
  ]
}
Related