How elasticsearch nested query work?

I have a document:

{
    "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:

{
    "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:

{
    "_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.

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