How to apply combination of filters on array of objects?

Hi Please help me to write a suitable query for my task, I have list of products with different categories and associated attribute and tags. Here is two documents for two categories along with associated attribute list. There could be multiple attributes, just showing one.

{
    "category": "blouses",
    "attributes": [
        {
            "attribute": "women-blouse-neckline",
            "tag": "round-neck"
        }
    ]
}

{
    "category": "dresses",
    "attributes": [
        {
            "attribute": "women-dress-neckline",
            "tag": "v-neck"
        }
    ]
}

Now customer wants get list of products from both categories which are dresses and blouses, but along with that a specific case at attribute level is :
Fetch all the products from dresses where attribute is women-dress-neckline and tag is v-neck along with all the products from blouses where attribute is women-blouse-neckline and tag is round-neck.
Thanks.

I believe below should work for you

{
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "category": ["dresses", "blouses"]
                    }
                }
            ],
            "should": [
                {
                    "match_phrase": {
                         "attribute": "women-dress-nechline"
                    }
                },
                {
                    "match_phrase": {
                         "attribute": "women-blouse-nechline"
                    }
                },
                {
                    "match_phrase": {
                         "tag": "v-neck"
                    }
                },
                {
                    "match_phrase": {
                         "tag": "round-neck"
                    }
                }
            ],
            "minimum_should_match": 2
        }    
    }
}

If not 100% you might need to play around with "filter"

Thanks @sholzhauer, This solution not works because there is no relation women-blouse-nechline with 'round-neck', This returns products from dresses as well who has tags round-neck

To avoid “cross-matching” problems on objects you should look into the “nested” field type and related query.

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