'[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]'

I am running the following query in Elasticsearch and for some reason I am getting an error:

elasticsearch.exceptions.RequestError: RequestError(400, 'x_content_parse_exception', '[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]')

My query structure

"_source":  ["file_id"],
    "min_score": minimun_score,
    "from": pagination_skip,
    "size": data["page_size"],
    "query": {
        "script_score": {
            "query": {
                "bool":{
                "must": [
                    {
                        "exists": {
                            "field": "image_vector"
                        }
                    }
                ]
                },
                "nested": {
                    "path": nested_field,
                    "query": {
                        "bool": {
                            "filter": [project_filter, users_filter, status_filter, date_filter]
                        }
                    },
                    "inner_hits": {
                        "highlight": {
                            "fields": {
                                "activity": {

                                }
                            }
                        }
                    }
                }
            },
            "script": {
                "source": "cosineSimilarity(params.query_vector, 'image_vector') + 0.0",
                "params": {
                    "query_vector": data["image_vector"]
                }
            }
        }
    }
}

The query was working until I hadn't add must clause. Basically, I don't want cosineSimilarity function to work upon those documents which doesn't contain image_vector field That's why I have used must clause so that only those documents are returned which contain image_vector field. Can anyone tell me what's wrong in the query structure or if my use-case can be solved with some other query, then that's also welcome.

I believe that error is here:

You have the "must" and "nested" in same level. One option is the "nested" inside bool query inside a must, filter, should or must_not clause.

      "query": {
        "bool": { <-----
          "must": [
            {
              "exists": {
                "field": "image_vector"
              }
            }
          ]
        },
        "nested": { <-----
          "path": "nested_field",
          "query": {
            "bool": {
              "filter": [
                "project_filter",
                "users_filter",
                "status_filter",
                "date_filter"
              ]
            }
          },
          "inner_hits": {
            "highlight": {
              "fields": {
                "activity": {}
              }
            }
          }
        }
      }

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