Combining nested query with bool query


(Velusamy Velu) #1

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.


(Mark Harwood) #2

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.


(Velusamy Velu) #3

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
}

(Mark Harwood) #4

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


(Velusamy Velu) #5

Thanks Mark, it worked!

I appreciate your help.


(system) #6

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