elasticsearchknn

Elastic Search | Error when Using KNN field in Bool query


I am trying to use knn in search API under bool query. But getting and error. I am using elastic search 8.6.2

Here is my query

GET document-with-embeddings/_search
{
    "query":
        {
            "bool": {
                "must": [
                  {
                    "knn": {
                               "text_embedding.predicted_value": {
                                 "vector": [
                                    -0.06544870883226395,
                                    -0.21647875010967255,
                                    ...................
                       ],
                                "k": 20
                               }
                                
                            }
                  }
                ],
                "filter": [],
                "should": [],
                "must_not": []
            }
        },
    "_source": [
    "name", "description" 
]
}

And my indexing for the embedding is

properties": {
                "text_embedding.predicted_value": {
                    "type": "dense_vector",
                    "dims": 384,
                    "index": true,
                    "similarity": "cosine"
                },

And I am getting this error.

{
  "error": {
    "root_cause": [
      {
        "type": "x_content_parse_exception",
        "reason": "[7:28] [bool] failed to parse field [must]"
      }
    ],
    "type": "x_content_parse_exception",
    "reason": "[7:28] [bool] failed to parse field [must]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "[knn] queries cannot be provided directly, use the [knn] body parameter instead"
    }
  },
  "status": 400
}

One point to add here, I will use a complex query. That's why I used bool. But a simple query as the below one works for me, which is not my goal.

GET document-with-embeddings/_search
{
"knn": {
    "field": "text_embedding.predicted_value",
    "query_vector": [...],
"k": 20,
    "num_candidates": 1000
},
"_source": [
    "custom"
]
}

Any help is appreciated.


Solution

  • The documentation shows this mode. The Knn must be used out of "query".

    POST image-index/_search
    {
      "query": {
        "match": {
          "title": {
            "query": "mountain lake",
            "boost": 0.9
          }
        }
      },
      "knn": {
        "field": "image-vector",
        "query_vector": [54, 10, -2],
        "k": 5,
        "num_candidates": 50,
        "boost": 0.1
      },
      "size": 10
    }