# How elasticsearch nested query work?

**URL:** <https://discuss.elastic.co/t/how-elasticsearch-nested-query-work/287821>\
**Category:** Elasticsearch\
**Created:** [October 27, 2021, 4:02pm UTC](https://discuss.elastic.co/t/how-elasticsearch-nested-query-work/287821 "2021-10-27T16:02:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tanvu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanvu/32/96318_2.png) [@tanvu](https://discuss.elastic.co/u/tanvu)\
**Post date:** [October 27, 2021, 4:02pm UTC](https://discuss.elastic.co/t/how-elasticsearch-nested-query-work/287821/1 "2021-10-27T16:02:41Z")

</div>

I have a document:

```auto
{
    "recipes": [
        {
            "name": "prepare",
            "ingredients": ["pork","beef"],
            "steps": [
                {
                    "ingredients": ["pork","beef"]
                }
            ]
        },
        {
            "name": "cook",
            "ingredients": ["olive","garlic","onion","salt"],
            "steps": [
                {
                    "ingredients": ["garlic","onion"]
                },
                {
                    "ingredients": ["olive"]
                },
                {
                    "ingredients": ["salt"]
                }
            ]
        }
    ]
}

```

and a mapping:

```auto
{
    "mappings": {
        "properties": {
            "recipes": {
                "type": "nested",
                "properties": {
                    "name": {
                        "type": "keyword"
                    },
                    "ingredients": {
                        "type": "keyword"
                    },
                    "steps": {
                        "type": "nested",
                        "properties": {
                            "ingredients": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

```

I want to ask what the behaviour of the nested query is, if I use a query like this:

```auto
{
    "_source": ["*.inner_hits"],
    "query": {
        "nested": {
            "path": "recipes.steps",
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "recipes.steps.ingredients": "garlic" (1)
                            }
                        },
                        {
                            "bool": {
                                "must": [
                                    {
                                        "nested": {
                                            "path": "recipes",
                                            "query": {
                                                "bool": {
                                                    "must": [
                                                        {
                                                            "term": {
                                                                "recipes.ingredients": "pork" (2)
                                                            }
                                                        },
                                                        {
                                                            "term": {
                                                                "recipes.ingredients": "beef" (3)
                                                            }
                                                        }
                                                    ]
                                                }
                                            },
                                            "inner_hits": {}
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            "inner_hits": {}
        }
    }
}

```

The query will return the document above, so it basically get the `garlic` from the second recipe, first step, i.e. `recipes[1]["steps"][0]["ingredients"]` , and the `pork` from the first recipe, i.e. `recipes[0]["ingredients"]` . But if I switch the (1) to `pork` , and (2) to `garlic` and (3) to `onion` , I expect it also returns the document, but it does not. So, how it really works?

Thank you.

---

<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:** [November 24, 2021, 4:02pm UTC](https://discuss.elastic.co/t/how-elasticsearch-nested-query-work/287821/2 "2021-11-24T16:02:47Z")

</div>

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