# '\[bool\] malformed query, expected \[END\_OBJECT\] but found \[FIELD\_NAME\]'

**URL:** <https://discuss.elastic.co/t/bool-malformed-query-expected-end-object-but-found-field-name/317862>\
**Category:** Elasticsearch\
**Created:** [November 1, 2022, 4:01am UTC](https://discuss.elastic.co/t/bool-malformed-query-expected-end-object-but-found-field-name/317862 "2022-11-01T04:01:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shobhit\_Jain1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shobhit_jain1/32/97834_2.png) [@Shobhit\_Jain1](https://discuss.elastic.co/u/Shobhit_Jain1)\
**Post date:** [November 1, 2022, 4:01am UTC](https://discuss.elastic.co/t/bool-malformed-query-expected-end-object-but-found-field-name/317862/1 "2022-11-01T04:01:56Z")

</div>

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

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

```

**My query structure**

```auto
"_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.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [November 1, 2022, 11:26am UTC](https://discuss.elastic.co/t/bool-malformed-query-expected-end-object-but-found-field-name/317862/2 "2022-11-01T11:26:11Z")

</div>

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.

```auto
      "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": {}
              }
            }
          }
        }
      }

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 29, 2022, 11:26am UTC](https://discuss.elastic.co/t/bool-malformed-query-expected-end-object-but-found-field-name/317862/3 "2022-11-29T11:26:52Z")

</div>

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