Elasticsearch malformed query, expected [END_OBJECT] but found [FIELD_NAME]

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

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:

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. :slightly_smiling_face:

2 Likes

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

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