# How to match multiple should clauses?

**URL:** <https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066>\
**Category:** Elasticsearch\
**Created:** [October 17, 2019, 1:20pm UTC](https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066 "2019-10-17T13:20:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![randomuser](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@randomuser](https://discuss.elastic.co/u/randomuser)\
**Post date:** [October 17, 2019, 1:20pm UTC](https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066/1 "2019-10-17T13:20:21Z")

</div>

Hello,

I'm looking for a way of matching multiple should cases. To explain this better, lets use this example:

```
"query": {
    "bool": {
        "must": [...]
        "should": [
            {
                "terms" : {
                    "fieldValues" : [1,5,9]
                 }
            },
            {
                "terms" : {
                    "fieldValues" : [2,4,8]
                 }
            }
         ]
    }
}

```

With this approach, the should part of the query matches:

> 1 OR 5 OR 9 OR 2 OR 4 OR 8

BUT I need the query to match:

> (1 OR 5 OR 9) AND (2 OR 4 OR 8)

How does one achieve this?

---

<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 17, 2019, 1:36pm UTC](https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066/2 "2019-10-17T13:36:37Z")

</div>

Hi.

Change the `should` to `must`. Your `terms` queries are an OR of their array contents already

---

<div class="post-metadata">

**Author:** ![randomuser](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@randomuser](https://discuss.elastic.co/u/randomuser)\
**Post date:** [October 17, 2019, 2:26pm UTC](https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066/3 "2019-10-17T14:26:38Z")

</div>

I've tested this before, even re-tried now to make sure I wasn't making a mistake, but if terms with an array of values is under a must filter, it doesn't match anything at all. Any ideas?

---

<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 17, 2019, 2:44pm UTC](https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066/4 "2019-10-17T14:44:46Z")

</div>

> [@randomuser](#):
>
> if terms with an array of values is under a must filter, it doesn't match anything at all

Can you supply a reproduction I can run in Kibana dev tools? Something like this (which works OK):

```
DELETE test
POST test/_doc/1
{
  "values":["a","e","i","o","u", "x","y","z"]
}
GET test/_search
{
  "query": {
	"bool":{
	  "must":[{
		"terms":{
		  "values":["a", "d"]
		}
	  },{
		"terms":{
		  "values":["w", "x"]
		}
	  }]
	}
  }
}

```

---

<div class="post-metadata">

**Author:** ![randomuser](https://avatars.discourse-cdn.com/v4/letter/r/958977/32.png) [@randomuser](https://discuss.elastic.co/u/randomuser)\
**Post date:** [October 17, 2019, 2:45pm UTC](https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066/5 "2019-10-17T14:45:34Z")

</div>

Might be the weird use-case I'm having here.

If you read the other post I made [[Theoretical] How does query filtering upon nested aggregation work?](https://discuss.elastic.co/t/theoretical-how-does-query-filtering-upon-nested-aggregation-work/204072), basically the third question might be the issue here..

- Does this part of the query:

Ps: If this still doesn't make sense, I'll try creating a reproduction of the problem so you can try running it.

---

<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 17, 2019, 2:50pm UTC](https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066/6 "2019-10-17T14:50:21Z")

</div>

Your use of terminology is confusing.  
Query clauses operate on values in individual documents in individual shards.  
Aggregations are summaries of many documents held on many shards.

The former filters the set of documents summarised by the latter.

> [@randomuser](#):
>
> Ps: If this still doesn't make sense, I'll try creating a reproduction of the problem

That sounds like it would be useful. As trimmed-down as possible please (so only has the fields of interest and the minimum number of docs).

---

<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 14, 2019, 2:50pm UTC](https://discuss.elastic.co/t/how-to-match-multiple-should-clauses/204066/7 "2019-11-14T14:50:29Z")

</div>

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