# Searching multiple values in multiple fields

**URL:** <https://discuss.elastic.co/t/searching-multiple-values-in-multiple-fields/241177>\
**Category:** Elasticsearch\
**Created:** [July 14, 2020, 5:46pm UTC](https://discuss.elastic.co/t/searching-multiple-values-in-multiple-fields/241177 "2020-07-14T17:46:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brsntus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brsntus/32/72139_2.png) [@brsntus](https://discuss.elastic.co/u/brsntus)\
**Post date:** [July 14, 2020, 5:46pm UTC](https://discuss.elastic.co/t/searching-multiple-values-in-multiple-fields/241177/1 "2020-07-14T17:46:57Z")

</div>

Hi folks.

I have an index with documents that have a `product_id` (long) and `product_subtype` (text). I'm trying to search for documents using multiple `product_id`s and `product_subtype`s.

The data that I have is this:

```auto
[
    {
        "id": 1,
        "product_id": 123,
        "product_subtype": "subtype abc"
    },
    {
        "id": 2,
        "product_id": 123,
        "product_subtype": "subtype def"
    },
    {
        "id": 3,
        "product_id": 456,
        "product_subtype": "subtype abc"
    },
    {
        "id": 4,
        "product_id": 789,
        "product_subtype": "subtype abc"
    },
    {
        "id": 5,
        "product_id": 789,
        "product_subtype": "subtype def"
    }
]

```

I want to search for products with `product_subtype = "subtype abc" OR product_subtype = "subtype def"`, and that belong to `product_id 123 OR 456`.  
The result I expect is ids 1, 2 and 3 from the data above.

My initial approach was to do this query, but this also returns ids 4 and 5:

```auto
{
    "query": {
        "bool": {
            "should": [
                { "match_phrase": { "product_subtype": "subtype abc" } },
                { "match_phrase": { "product_subtype": "subtype def" } },
                { "terms": { "product_id": [123, 456] } }
            ]
        }
    }
}

```

I then tried to move the `product_id` part to `must` instead of `should`. This works if I query for both `product_subtype`s (`abc` and `def`), but if I query for just `abc`, it still returns results for `def` (although now respecting the `product_id`s):

```auto
{
    "query": {
        "bool": {
            "should": [
                { "match_phrase": { "product_subtype": "subtype abc" } },
                { "match_phrase": { "product_subtype": "subtype def" } }
            ],
            "must": [
                { "terms": { "product_id": [123, 456] } }
            ]
        }
    }
}

```

The last thing I tried was moving everything to `must`, but then it doesn't return anything.

Is there a way that I could accomplish this query with ES?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![brsntus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brsntus/32/72139_2.png) [@brsntus](https://discuss.elastic.co/u/brsntus)\
**Post date:** [July 14, 2020, 7:10pm UTC](https://discuss.elastic.co/t/searching-multiple-values-in-multiple-fields/241177/2 "2020-07-14T19:10:37Z")

</div>

Guess I solved my own problem with this query below:

```auto
{
    "query": {
        "bool": {
            "must": [
                { "terms": { "product_id": [123, 456] } },
                {
                    "bool": {
                        "should": [
                            { "match_phrase": { "product_subtype": "subtype abc" } },
                            { "match_phrase": { "product_subtype": "subtype def" } }
                        ]
                    }
                }
            ]
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)\
**Post date:** [July 14, 2020, 7:15pm UTC](https://discuss.elastic.co/t/searching-multiple-values-in-multiple-fields/241177/3 "2020-07-14T19:15:30Z")

</div>

@brsntus  
You may want to use filter instead of must and `{ "term": { "product_subtype.keyword" : "subtype abc"} }` instead of match\_phrase. I am assuming product\_subtype has keyword as subfield.

Your match\_phrase will also match " foo subtype abc def"

---

<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:** [August 11, 2020, 7:15pm UTC](https://discuss.elastic.co/t/searching-multiple-values-in-multiple-fields/241177/4 "2020-08-11T19:15:41Z")

</div>

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