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

**URL:** <https://discuss.elastic.co/t/terms-malformed-query-expected-end-object-but-found-field-name/128765>\
**Category:** Elasticsearch\
**Created:** [April 19, 2018, 7:10pm UTC](https://discuss.elastic.co/t/terms-malformed-query-expected-end-object-but-found-field-name/128765 "2018-04-19T19:10:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Syed\_ali\_abbas\_shah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syed_ali_abbas_shah/32/30053_2.png) [@Syed\_ali\_abbas\_shah](https://discuss.elastic.co/u/Syed_ali_abbas_shah)\
**Post date:** [April 19, 2018, 7:10pm UTC](https://discuss.elastic.co/t/terms-malformed-query-expected-end-object-but-found-field-name/128765/1 "2018-04-19T19:10:25Z")

</div>

Here is my mapping

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

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

---

<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:** [April 20, 2018, 1:35pm UTC](https://discuss.elastic.co/t/terms-malformed-query-expected-end-object-but-found-field-name/128765/2 "2018-04-20T13:35:38Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html). Your query would look something like this:

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

```

---

<div class="post-metadata">

**Author:** ![Syed\_ali\_abbas\_shah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syed_ali_abbas_shah/32/30053_2.png) [@Syed\_ali\_abbas\_shah](https://discuss.elastic.co/u/Syed_ali_abbas_shah)\
**Post date:** [April 20, 2018, 3:50pm UTC](https://discuss.elastic.co/t/terms-malformed-query-expected-end-object-but-found-field-name/128765/3 "2018-04-20T15:50:44Z")

</div>

> [@abdon](#):
>
> 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"  
> ]  
> }  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }

Many THanks

---

<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:** [May 18, 2018, 3:51pm UTC](https://discuss.elastic.co/t/terms-malformed-query-expected-end-object-but-found-field-name/128765/4 "2018-05-18T15:51:03Z")

</div>

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