# Nested query with must clause and second nested query

**URL:** <https://discuss.elastic.co/t/nested-query-with-must-clause-and-second-nested-query/273380>\
**Category:** Elasticsearch\
**Created:** [May 19, 2021, 9:52am UTC](https://discuss.elastic.co/t/nested-query-with-must-clause-and-second-nested-query/273380 "2021-05-19T09:52:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hastinc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hastinc/32/87473_2.png) [@hastinc](https://discuss.elastic.co/u/hastinc)\
**Post date:** [May 19, 2021, 9:52am UTC](https://discuss.elastic.co/t/nested-query-with-must-clause-and-second-nested-query/273380/1 "2021-05-19T09:52:11Z")

</div>

Hi All,  
First of all sorry for the long post.

I'm wondering if somebody can confirm if the query I've constructed is correct.

Basically, I have a `User` mapping that has `Transaction` nested within the user and I'm looking for Users that bought a specific item before a date. Also, within a `transaction` I also have `line_items` which is also a nested field.

Originally, I constructed the following query, and based on the data that was returned I've concluded the query is probably incorrect.

```json
{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "transaction",
                        "query": {
                            "nested": {
                                "path": "transaction.line_items",
                                "query": {
                                    "bool": {
                                        "must": {
                                            "match": {
                                                "transaction.line_items.barcode": {
                                                    "query": "abc123xyz"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "transaction",
                        "query": {
                            "range": {
                                "transaction.timestamp": {
                                    "from": null,
                                    "include_lower": true,
                                    "include_upper": true,
                                    "to": "2021-05-14T00:00:00+02:00"
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

```

I then updated my query and now based on the results returned I _think_ the query is correct. However, in order to avoid confirmation bias, I'm wondering if somebody can explain why the second query is correct (assuming it is).

```json
{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "transaction",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "range": {
                                            "transaction.timestamp": {
                                                "from": null,
                                                "include_lower": true,
                                                "include_upper": true,
                                                "to": "2021-05-16T00:00:00+02:00"
                                            }
                                        }
                                    },
                                    {
                                        "nested": {
                                            "path": "transaction.line_items",
                                            "query": {
                                                "bool": {
                                                    "must": {
                                                        "match": {
                                                            "transaction.line_items.barcode": {
                                                                "query": "abc123xyz"
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

```

---

<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:** [June 16, 2021, 9:52am UTC](https://discuss.elastic.co/t/nested-query-with-must-clause-and-second-nested-query/273380/2 "2021-06-16T09:52:53Z")

</div>

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