utkarsh404
utkarsh404

Reputation: 870

BadRequestError: BadRequestError(400, 'x_content_parse_exception', '[1:34] [bool] failed to parse field [must]')

My query shows the error BadRequestError: BadRequestError(400, 'x_content_parse_exception', '[1:34] [bool] failed to parse field [must]')

I tried removing [] but this too, didn't work. I feel I messed up with KNN part in this query. Please help!

helper_token = Tokenizer()
INPUT = input("Enter the Input Query ")
token_vector = helper_token.get_token(INPUT)

query ={
  
   "size":50,
   "_source": "Title", 
   "query":{
      "bool":{
         "must":[
            {
               "knn":{
                  "vectors":{
                     "vector":token_vector,
                     "k":20
                  }
               }
            }
         ]
      }
   }
}
es = Elasticsearch(timeout=600,hosts=os.getenv(ENDPOINT),cloud_id="PVT",http_auth=("elastic", PASSWORD))
res = es.search(index='posting',body=query)

title = [x['_source']  for x in res['hits']['hits']]
title

Upvotes: 1

Views: 1772

Answers (2)

Jakob
Jakob

Reputation: 31

I managed to make a successful knn query with the elastic python client like this:

    query = {
                "field": "<name of field that contains the vector>",
                "query_vector": query_vector,
                "k": 10,
                "num_candidates": 50
    }
              
    response = es.search(index="<index name>", knn=query)

and then you can get the title field like this:

hits = response['hits']['hits']
for hit in hits:
    title = hit['_source']['title']

I hope this helps.

Upvotes: 0

Val
Val

Reputation: 217464

If you're on 8.4, the knn query needs to be specified like this (i.e. knn is a sibling of query):

query ={
  
   "size":50,
   "_source": "Title", 
   "knn":{
     "vectors":{
       "vector":token_vector,
       "k":20
     }
   }
}

Upvotes: 0

Related Questions