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

**URL:** <https://discuss.elastic.co/t/elasticsearch-malformed-query-expected-end-object-but-found-field-name/206050>\
**Category:** Elasticsearch\
**Created:** [October 31, 2019, 2:01pm UTC](https://discuss.elastic.co/t/elasticsearch-malformed-query-expected-end-object-but-found-field-name/206050 "2019-10-31T14:01:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![111230](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111230/32/47196_2.png) [@111230](https://discuss.elastic.co/u/111230)\
**Post date:** [October 31, 2019, 2:01pm UTC](https://discuss.elastic.co/t/elasticsearch-malformed-query-expected-end-object-but-found-field-name/206050/1 "2019-10-31T14:01:01Z")

</div>

My query looks like this

{  
"query": {  
"bool": {  
"must": [  
{  
"has\_parent": {  
"parent\_type": "doc",  
"query": {  
"bool": {  
"must": [  
{  
"terms": {  
"id": [  
713  
]}},  
{  
"range": {  
"created": {  
"lte": "now/d"  
}}},  
{  
"range": {"expires": { "gte": "now/d"  
}}}]  
}}}},  
{  
"term": {  
"doc\_type": "item"  
}},  
{"bool": {"should": [  
{  
"term": {  
"have\_prices": true}},  
{  
"term": {  
"is\_folder": true  
}}]}}],  
"must\_not": {  
"exists": {  
"field": "folder"  
}}}  
},  
"sort": [  
{  
"is\_folder": {  
"order": "desc"}},  
{  
"title\_low.order": {  
"order": "asc"  
}  
}  
],  
"size": 1000  
}

and I get some result, like this one

"hits": {  
"total": 19,  
"max\_score": null,  
"hits": [  
{  
"\_index": "prices",  
"\_type": "doc",  
"\_id": "item-6800004",  
"\_score": null,  
"\_routing": "1",  
"\_source": {  
"id": 6800004,  
"id\_pricedoc": 713,  
"title": ""водка №1" 1",  
"title\_low": ""водка №1" 1",  
"supplier": {  
"id": 7831,  
"type": null  
},  
"supplier\_nom": {  
"id": 1375697,  
"market\_nom": {  
"id": null  
},  
"codes": null,  
"sup\_code": "7a6713a5-73c1-3acb-9b62-9e38b2314dce",  
"manufacturer": {  
"id": null,  
"title": null  
}  
},  
"is\_folder": false,  
"folder": null,  
"path": null,  
"pricedoc\_created": "2016-03-21",  
"prices": [  
{  
"currency": "RUR",  
"id\_offer": 15735967,  
"id\_prcknd": 167,  
"value": "391.50"  
}  
],  
"have\_prices": true,  
"market": {  
"title": null,  
"uuid": null,  
"folder": null,  
"path": null  
},  
"\_join\_field\_name": "doc\_type",  
"doc\_type": {  
"name": "item",  
"parent": "doc-713"  
}  
},  
"sort": [  
0,  
""водка №1" 1"  
]  
}

Now I would like to modify query that will matches docs witch only "id\_prcknd": 167. I wrote something like this  
{  
"query": {  
"bool": {  
"must": [  
{  
"has\_parent": {  
"parent\_type": "doc",  
"query": {  
"bool": {  
"must": [  
{  
"terms": {  
"id": [  
713  
]  
}  
},  
{  
"range": {  
"created": {  
"lte": "now/d"  
}  
}  
},  
{  
"range": {  
"expires": {  
"gte": "now/d"  
}}}]}}}},  
{  
"term": {  
"doc\_type": "item"  
}  
},  
{  
"bool": {  
"should": [  
{  
"term": {  
"have\_prices": true  
}  
},  
{  
"term": {  
"is\_folder": true  
}}]}  
}  
],  
"must\_not": {  
"exists": {  
"field": "folder"  
}  
}  
},  
"nested": {  
"path": "prices",  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"prices.id\_prcknd": 167  
}}]}  
}  
},  
"sort": [  
{  
"is\_folder": {  
"order": "desc"  
}  
},  
{  
"title\_low.order": {  
"order": "asc"  
}  
}  
],  
"size": 1000  
}

But I got an error Elasticsearch malformed query, expected [END\_OBJECT] but found [FIELD\_NAME] Where am I wrong? I wanna match objects where "id\_prcknd": 167

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 4, 2019, 9:32pm UTC](https://discuss.elastic.co/t/elasticsearch-malformed-query-expected-end-object-but-found-field-name/206050/2 "2019-11-04T21:32:36Z")

</div>

You can't just stick a `nested` query inside the `query` clause. It has to be part of one of the `bool` query's clauses - in your case the `must` clause.

By the way, instead of `must` you may want to consider using `filter`. It will be faster as it does not cause Elasticsearch to calculate scores, and it may lead to caching.

The resulting query would become:

```auto
GET my_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "has_parent": {
            "parent_type": "doc",
            "query": {
              "bool": {
                "filter": [
                  {
                    "terms": {
                      "id": [
                        713
                      ]
                    }
                  },
                  {
                    "range": {
                      "created": {
                        "lte": "now/d"
                      }
                    }
                  },
                  {
                    "range": {
                      "expires": {
                        "gte": "now/d"
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "term": {
            "doc_type": "item"
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "have_prices": true
                }
              },
              {
                "term": {
                  "is_folder": true
                }
              }
            ]
          }
        },
        {
          "nested": {
            "path": "prices",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "prices.id_prcknd": 167
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "must_not": {
        "exists": {
          "field": "folder"
        }
      }
    }
  },
  "sort": [
    {
      "is_folder": {
        "order": "desc"
      }
    },
    {
      "title_low.order": {
        "order": "asc"
      }
    }
  ],
  "size": 1000
}

```

Also, please format your code in this forum using the `</>` button. It makes it much easier to read and as a result it's much more likely someone will help you. 🙂

---

<div class="post-metadata">

**Author:** ![111230](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111230/32/47196_2.png) [@111230](https://discuss.elastic.co/u/111230)\
**Post date:** [November 5, 2019, 7:09am UTC](https://discuss.elastic.co/t/elasticsearch-malformed-query-expected-end-object-but-found-field-name/206050/3 "2019-11-05T07:09:36Z")

</div>

Yes, i fugure it out, thank you for your answer! And sorry for code format(

---

<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:** [December 3, 2019, 7:09am UTC](https://discuss.elastic.co/t/elasticsearch-malformed-query-expected-end-object-but-found-field-name/206050/4 "2019-12-03T07:09:54Z")

</div>

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