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

Here is my mapping

{
  "mappings": {
    "movie": {
      "properties": {
        "film_to_franchise": {
          "type": "join",
          "relations": {
            "franchise": "film",
            "film":"cast"
          }
        },
        "id": {
          "type": "text"
        },
        "is_private":{
            "type":"boolean",
            "index":true
        },
        "genre":{
          "type": "text",
          "index": true
        },
        "title":{
          "type": "text",
          "index": true
        },
        "franchise_name":{
           "type": "text",
          "index": true
        },
        "cast_name":{
          "type": "text",
          "index": true
        },
        "movie_name":{
          "type": "text",
          "index": true
        }
      }
    }
  }
}```

But I want to peform query in 3 levels, like those 3rd level values should come where parent = 2 and grand_parent = 1

here is my query

```GET series/movie/_search
{
  "_source": [
    "id",
    "title",
    "cast_name",
    "film_to_franchise"
  ],
  "query": {
    "has_parent": {
      "parent_type": "film",
      "inner_hits": {
        "_source": [
          "title",
          "movie_name"
        ]
      },
      "query": {
         "terms": {
              "id": [
                "fr2"
              ]
            },
        "has_parent": {
          "parent_type": "franchise",
          "inner_hits": {
            "_source": [
              "title",
              "franchise_name"
            ]
          },
          "query": {
            "terms": {
              "id": [
                "fr2"
              ]
            }
          }
        }
      }
    }
  }
}

but it gives me following error

  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[terms] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 23,
        "col": 9
      }
    ],
    "type": "parsing_exception",
    "reason": "[terms] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 23,
    "col": 9
  },
  "status": 400
}```

You can't just stick a terms and a has_parent query together in a single query clause.

If you want to combine multiple queries, you need to use a compound query like a bool query. Your query would look something like this:

GET series/movie/_search
{
  "_source": [
    "id",
    "title",
    "cast_name",
    "film_to_franchise"
  ],
  "query": {
    "has_parent": {
      "parent_type": "film",
      "inner_hits": {
        "_source": [
          "title",
          "movie_name"
        ]
      },
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "id": [
                  "fr2"
                ]
              }
            },
            {
              "has_parent": {
                "parent_type": "franchise",
                "inner_hits": {
                  "_source": [
                    "title",
                    "franchise_name"
                  ]
                },
                "query": {
                  "terms": {
                    "id": [
                      "fr2"
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
1 Like

Many THanks

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