# Combining nested query with bool query

**URL:** <https://discuss.elastic.co/t/combining-nested-query-with-bool-query/94701>\
**Category:** Elasticsearch\
**Created:** [July 26, 2017, 8:06pm UTC](https://discuss.elastic.co/t/combining-nested-query-with-bool-query/94701 "2017-07-26T20:06:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 26, 2017, 8:06pm UTC](https://discuss.elastic.co/t/combining-nested-query-with-bool-query/94701/1 "2017-07-26T20:06:02Z")

</div>

I'm using ElasticSearch 5.5.1. I want to query a document that has nested objects. I will quote a popular example documented well in the ElasticSearch community.

The nested documents are:

```
[  
   {  
      "name":"Bob",
      "car":[ <!-- Nested -->
         {  
            "make":"Saturn",
            "model":"Imprezza"
         }
      ]
   },
   {  
      "name":"Zach",
      "car":[ <!-- Nested -->  
         {  
            "make":"Saturn",
            "model":"SL"
         },
         {  
            "make":"Subaru",
            "model":"Imprezza"
         }
      ]
   }
]

```

The query:

```
{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "name": {
                     "value": "bob"
                  }
               }
            }
         ]
      }
   }
}

```

Works as expected and the nested query:

```
{
   "query": {
      "nested": {
         "path": "car",
         "query": {
            "bool": {
               "must": [
                  {
                     "match": {
                        "car.make": "Saturn"
                     }
                  },
                  {
                     "match": {
                        "car.model": "SL"
                     }
                  }
               ]
            }
         }
      }
   }
}

```

Works well too. However, the combination of the above two queries fails to work.

```
{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "name": {
                     "value": "bob"
                  }
               }
            }
         ]
      },
      "nested": {
         "path": "car",
         "query": {
            "bool": {
               "must": [
                  {
                     "match": {
                        "car.make": "Saturn"
                     }
                  },
                  {
                     "match": {
                        "car.model": "SL"
                     }
                  }
               ]
            }
         }
      }
   }
}

```

It results in the following error message.

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

```

Any idea what could be the problem here? Your help will be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 26, 2017, 8:12pm UTC](https://discuss.elastic.co/t/combining-nested-query-with-bool-query/94701/2 "2017-07-26T20:12:32Z")

</div>

Your bool and your nested query are at the same level. When they sit side by side like that you have given no indication how they should be combined (ANDed? ORed?) - that's why we throw an error.  
Put your nested query alongside the term query in the array of `must` clauses which all must be satisfied or a match.

---

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 26, 2017, 9:50pm UTC](https://discuss.elastic.co/t/combining-nested-query-with-bool-query/94701/3 "2017-07-26T21:50:40Z")

</div>

Mark Harwood, thank you for your response. I think you meant to create a query like this:

```
{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "name": {
                     "value": "bob"
                  }
               }
            }
         ],
         "nested": {
            "path": "car",
            "query": {
               "bool": {
                  "must": [
                     {
                        "match": {
                           "car.make": "Saturn"
                        }
                     },
                     {
                        "match": {
                           "car.model": "SL"
                        }
                     }
                  ]
               }
            }
         }
      }
   }
}

```

It's nor supported by ElasticSearch. Here's the result

```
{
   "error": {
      "root_cause": [
         {
            "type": "parsing_exception",
            "reason": "[bool] query does not support [nested]",
            "line": 13,
            "col": 20
         }
      ],
      "type": "parsing_exception",
      "reason": "[bool] query does not support [nested]",
      "line": 13,
      "col": 20
   },
   "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 26, 2017, 9:53pm UTC](https://discuss.elastic.co/t/combining-nested-query-with-bool-query/94701/4 "2017-07-26T21:53:18Z")

</div>

> [@Mark\_Harwood](#):
>
> in the array of must clauses

Your nested query is not in the array of must clauses.

---

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 26, 2017, 10:18pm UTC](https://discuss.elastic.co/t/combining-nested-query-with-bool-query/94701/5 "2017-07-26T22:18:44Z")

</div>

Thanks Mark, it worked!

I appreciate your help.

---

<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:** [August 23, 2017, 10:18pm UTC](https://discuss.elastic.co/t/combining-nested-query-with-bool-query/94701/6 "2017-08-23T22:18:55Z")

</div>

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