# Not working: Must query between array of records in nested type

**URL:** <https://discuss.elastic.co/t/not-working-must-query-between-array-of-records-in-nested-type/223222>\
**Category:** Elasticsearch\
**Created:** [March 11, 2020, 9:22pm UTC](https://discuss.elastic.co/t/not-working-must-query-between-array-of-records-in-nested-type/223222 "2020-03-11T21:22:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepak643](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepak643/32/45940_2.png) [@deepak643](https://discuss.elastic.co/u/deepak643)\
**Post date:** [March 11, 2020, 9:22pm UTC](https://discuss.elastic.co/t/not-working-must-query-between-array-of-records-in-nested-type/223222/1 "2020-03-11T21:22:20Z")

</div>

I have the below record:

```
{
        "objectId": "ea5f9b14e0ca03990591de06a433a220",
        "annotations": [
            {
                "itemDetails": {
                    "value": "US1",
                    "confidence": 1
                }
            },
            {
                "itemDetails": {
                    "value": "US2",
                    "confidence": 0.9579
                }
            }
        ]
    }

```

**annotations** is nested in the above document. and value is **keyword** in itemDetails.

I am using the below query to fetch this record. But no results found.

```
{
   "query": {
      "nested": {
         "path": "annotations",
         "query": {
            "bool": {
               "must": [
                  {
                     "match": {
                        "annotations.itemDetails.value": "US1"
                     }
                  },
                  {
                     "match": {
                        "annotations.itemDetails.value": "US2"
                     }
                  }
               ]
            }
         }
      }
   }
}

```

Need suggestion to understand what I am doing wrong. **should** operation is working file but not sure whats wrong with the **must**

**Update** :  
Correct me if I am wrong, From this link [https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html), I see must inside the nested record will not work. Page says --\> This query doesn’t match because `US1` and `US2` are not in the same nested object.

Big trouble now. How can I achieve the above by changing the mappings?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 12, 2020, 10:33am UTC](https://discuss.elastic.co/t/not-working-must-query-between-array-of-records-in-nested-type/223222/2 "2020-03-12T10:33:51Z")

</div>

Change the `must` part to a `should` part, which resembles an `OR` as long as you do not add a `filter/must_not/must` part to that bool query.

---

<div class="post-metadata">

**Author:** ![deepak643](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deepak643/32/45940_2.png) [@deepak643](https://discuss.elastic.co/u/deepak643)\
**Post date:** [March 12, 2020, 11:34pm UTC](https://discuss.elastic.co/t/not-working-must-query-between-array-of-records-in-nested-type/223222/3 "2020-03-12T23:34:05Z")

</div>

I found below query solved the problem. But I am mentioning "path" twice here. What are the consequences or issues I face if I do so?

```
{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "annotations",
                        "query": {
                            "bool": {
                                "must": [
                                    {"match": {"annotations.itemDetails.value": "US1"}}
                                ]
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "annotations",
                        "query": {
                            "bool": {
                                "must": [
                                    {"match": {
                                           "annotations.itemDetails.value": "US2"}}
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}
```

---

<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 9, 2020, 11:34pm UTC](https://discuss.elastic.co/t/not-working-must-query-between-array-of-records-in-nested-type/223222/4 "2020-04-09T23:34:08Z")

</div>

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