# Combined nested query returning wrong result

**URL:** <https://discuss.elastic.co/t/combined-nested-query-returning-wrong-result/289612>\
**Category:** Elasticsearch\
**Created:** [November 18, 2021, 2:57pm UTC](https://discuss.elastic.co/t/combined-nested-query-returning-wrong-result/289612 "2021-11-18T14:57:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![identify](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/identify/32/97272_2.png) [@identify](https://discuss.elastic.co/u/identify)\
**Post date:** [November 18, 2021, 2:57pm UTC](https://discuss.elastic.co/t/combined-nested-query-returning-wrong-result/289612/1 "2021-11-18T14:57:50Z")

</div>

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:

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

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

---

<div class="post-metadata">

**Author:** ![identify](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/identify/32/97272_2.png) [@identify](https://discuss.elastic.co/u/identify)\
**Post date:** [November 18, 2021, 3:56pm UTC](https://discuss.elastic.co/t/combined-nested-query-returning-wrong-result/289612/2 "2021-11-18T15:56:51Z")

</div>

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

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

```

---

<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:** [December 16, 2021, 3:56pm UTC](https://discuss.elastic.co/t/combined-nested-query-returning-wrong-result/289612/3 "2021-12-16T15:56:53Z")

</div>

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