# Return documents where exact number of conditions in should clause(Bool Query) are matched

**URL:** <https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052>\
**Category:** Elasticsearch\
**Created:** [April 11, 2017, 9:08pm UTC](https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052 "2017-04-11T21:08:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [April 11, 2017, 9:08pm UTC](https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052/1 "2017-04-11T21:08:38Z")

</div>

Hi,

The "minimum\_number\_should\_match" property allows us to specify a minimum number of conditions in the should clause of the bool query that need to be satisfied. So if there are 4 conditions in the should clause and "minimum\_number\_should\_match" is set to 2, then any document where 2 or 3 or all 4 of the conditions are true is returned in the result set.

Is there a way to specify that the query should return only those documents where exactly 2 of the 4 conditions in the should clause are satisfied(In exactly 2 situation, a document where 3 or 4 conditions are satisfied will not be returned )?

Thanks.

---

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [April 24, 2017, 2:21pm UTC](https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052/2 "2017-04-24T14:21:50Z")

</div>

Bump

---

<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:** [April 24, 2017, 4:43pm UTC](https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052/3 "2017-04-24T16:43:09Z")

</div>

Ugly but how about should min\_should\_match of x and a must\_not clause of the same query clauses but with min\_should\_match of X+1 e.g.

```
DELETE test
POST /test/product/_bulk
{"index": {}}
{"codes": [1,2,3]}
{"index": {}}
{"codes": [1,2,8,9]}
{"index": {}}
{"codes": [2,3,8,9]}

GET test/product/_search
{
  "query": {
	"bool": {
	  "minimum_should_match": 2,
	  "should": [
		{
		  "term": {
			"codes": 1
		  }
		},
		{
		  "term": {
			"codes": 2
		  }
		},
		{
		  "term": {
			"codes": 3
		  }
		}
	  ],
	  "must_not": [
		{
		  "bool": {
			"minimum_should_match": 3,
			"should": [
			  {
				"term": {
				  "codes": 1
				}
			  },
			  {
				"term": {
				  "codes": 2
				}
			  },
			  {
				"term": {
				  "codes": 3
				}
			  }
			]
		  }
		}
	  ]
	}
  }
}
```

---

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [April 24, 2017, 4:53pm UTC](https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052/4 "2017-04-24T16:53:33Z")

</div>

Hi Mark,

Thanks for your reply! I think this solution would work for my problem.

Maybe as a future improvement to the bool query, we could have a "exact\_number\_should\_match" attribute or a "maximum\_number\_should\_match" just like the "minimum\_should\_match" attribute...that would save the user from writing the exact same query in the "should" and "must\_not" blocks.

Thanks!

---

<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:** [April 24, 2017, 5:27pm UTC](https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052/5 "2017-04-24T17:27:24Z")

</div>

It's a rare requirement. Can you share a use case that would help justify the need?

---

<div class="post-metadata">

**Author:** ![Rory](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@Rory](https://discuss.elastic.co/u/Rory)\
**Post date:** [April 24, 2017, 5:42pm UTC](https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052/6 "2017-04-24T17:42:21Z")

</div>

Maybe it is a rare requirement. But I have a use case where users can specify that they want exactly m out of n conditions to be true, where m \< n and n\<=10. This system is currently implemented in SQL and is very slow and we are in the process of migrating it to ElasticSearch...

---

<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:** [May 22, 2017, 5:55pm UTC](https://discuss.elastic.co/t/return-documents-where-exact-number-of-conditions-in-should-clause-bool-query-are-matched/82052/7 "2017-05-22T17:55:43Z")

</div>

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