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


#1

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.


#2

Bump


(Mark Harwood) #3

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
				}
			  }
			]
		  }
		}
	  ]
	}
  }
}

#4

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!


(Mark Harwood) #5

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


#6

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...


(system) #7

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