# Use Query Bool with Query Nested

**URL:** <https://discuss.elastic.co/t/use-query-bool-with-query-nested/125204>\
**Category:** Elasticsearch\
**Created:** [March 22, 2018, 1:50pm UTC](https://discuss.elastic.co/t/use-query-bool-with-query-nested/125204 "2018-03-22T13:50:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [March 22, 2018, 1:50pm UTC](https://discuss.elastic.co/t/use-query-bool-with-query-nested/125204/1 "2018-03-22T13:50:42Z")

</div>

Is possible use query \> bool together with query \> nested?

I have two types of queries, each one returns the results.  
Query \> Bool

```
{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "account": 14492
                    }
                },
                {
                    "term":{
                        "customer": 12345
                    }
                }
            ]
        }
        
    }
}

```

Query \> Nested

```
{
  "query": {
    "nested": {
       "path": "customfield_open.content",
       "query": {
         "bool": {
           "must": [
             {
               "match": {
                 "customfield_open.content.id": 8914
               }
             },
             {
               "match": {
                 "customfield_open.content.value": "RS"
               }
             }
          ]
        }
      }
    }
  }
}

```

Merged Querys:

```
{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "id":14492
                    }
                },
                {
                    "term":{
                        "customer": 12345
                    }
                }
            ]
        },
        "nested":{
            "path":"customfield_open.content",
            "query":{
                "bool":{
                    "must":[
                        {
                            "match":{
                                "customfield_open.content.id":8914
                            }
                        },
                        {
                            "match":{
                                "customfield_open.content.value":"RS"
                            }
                        }
                    ]
                }
            }
        }
    }
}

```

Error:

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

---

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [March 22, 2018, 2:07pm UTC](https://discuss.elastic.co/t/use-query-bool-with-query-nested/125204/2 "2018-03-22T14:07:47Z")

</div>

Solution:

```
{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "id":14492
                    }
                },
                {
                    "term":{
                        "customer": 12345
                    }
                },
                {
                    "nested":{
                        "path":"customfield_open.content",
                        "query":{
                            "bool":{
                                "must":[
                                    {
                                        "match":{
                                            "customfield_open.content.id":8914
                                        }
                                    },
                                    {
                                        "match":{
                                            "customfield_open.content.value":"RS"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}
```

---

<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:** [April 19, 2018, 2:08pm UTC](https://discuss.elastic.co/t/use-query-bool-with-query-nested/125204/3 "2018-04-19T14:08:04Z")

</div>

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