Combined nested query returning wrong result

Hello,

I am trying to do a query with two conditions within the "must" part of a "bool" query. Each condition is a nested query. The query is as follows:

{
   "query":{
      "bool":{
         "filter":[
            {
               "nested":{
                  "path":"attributes",
                  "query":{
                     "bool":{
                        "must":[
                           {
                              "match":{
                                 "attributes.key":"ATT001"
                              }
                           }
                        ]
                     }
                  }
               }
            },
            {
               "nested":{
                  "path":"attributes.details",
                  "query":{
                     "bool":{
                        "must":[
                           {
                              "match":{
                                 "attributes.details.locales":"en-GB"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

The result of my query is as follows (some data snipped out):

{
   "hits":{
      "total":{
         "value":1,
         "relation":"eq"
      },
      "max_score":0,
      "hits":[
         {
            "_source":{
               "attributes":[
                  {
                     "key":"ATT001",
                     "details":[
                        {
                           "locales":[
                              "de-DE"
                           ]
                        }
                     ]
                  }
               ]
            }
         }
      ]
   }
}

I would expect not having this item in my result, as I think the query is literally a "attributes.key = 'ATT001' AND attributes.details.locales = 'en-GB'"

OK - found the solution. Though to me my initial query appeared correct in terms of "read & understand". However the solution is as follows:

{
   "query":{
      "nested":{
         "path":"attributes",
         "query":{
            "bool":{
               "must":[
                  {
                     "match":{
                        "attributes.key":{
                           "query":"ATT001"
                        }
                     }
                  },
                  {
                     "nested":{
                        "path":"attributes.details",
                        "query":{
                           "bool":{
                              "should":[
                                 {
                                    "match":{
                                       "attributes.details.locales":{
                                          "query":"en-GB"
                                       }
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

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