# How to apply combination of filters on array of objects?

**URL:** <https://discuss.elastic.co/t/how-to-apply-combination-of-filters-on-array-of-objects/286946>\
**Category:** Elasticsearch\
**Created:** [October 18, 2021, 5:09am UTC](https://discuss.elastic.co/t/how-to-apply-combination-of-filters-on-array-of-objects/286946 "2021-10-18T05:09:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsharma2412](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@nsharma2412](https://discuss.elastic.co/u/nsharma2412)\
**Post date:** [October 18, 2021, 5:09am UTC](https://discuss.elastic.co/t/how-to-apply-combination-of-filters-on-array-of-objects/286946/1 "2021-10-18T05:09:44Z")

</div>

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.

```auto
{
    "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.

---

<div class="post-metadata">

**Author:** ![sholzhauer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sholzhauer/32/110282_2.png) [@sholzhauer](https://discuss.elastic.co/u/sholzhauer)\
**Post date:** [October 18, 2021, 7:23am UTC](https://discuss.elastic.co/t/how-to-apply-combination-of-filters-on-array-of-objects/286946/2 "2021-10-18T07:23:19Z")

</div>

I believe below should work for you

```json
{
    "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"

---

<div class="post-metadata">

**Author:** ![nsharma2412](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@nsharma2412](https://discuss.elastic.co/u/nsharma2412)\
**Post date:** [October 19, 2021, 3:26am UTC](https://discuss.elastic.co/t/how-to-apply-combination-of-filters-on-array-of-objects/286946/3 "2021-10-19T03:26:17Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 19, 2021, 7:30am UTC](https://discuss.elastic.co/t/how-to-apply-combination-of-filters-on-array-of-objects/286946/4 "2021-10-19T07:30:47Z")

</div>

> [@nsharma2412](#):
>
> 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.

---

<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 16, 2021, 7:31am UTC](https://discuss.elastic.co/t/how-to-apply-combination-of-filters-on-array-of-objects/286946/5 "2021-11-16T07:31:11Z")

</div>

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