File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -208,8 +208,11 @@ def __and__(self, other):
208208 del q ._params ["minimum_should_match" ]
209209
210210 for qx in (self , other ):
211- # TODO: percentages will fail here
212211 min_should_match = qx ._min_should_match
212+ # TODO: percentages or negative numbers will fail here
213+ # for now we report an error
214+ if not isinstance (min_should_match , int ) or min_should_match < 0 :
215+ raise ValueError ("Can only combine queries with positive integer values for minimum_should_match" )
213216 # all subqueries are required
214217 if len (qx .should ) <= min_should_match :
215218 q .must .extend (qx .should )
Original file line number Diff line number Diff line change @@ -278,6 +278,28 @@ def test_bool_and_bool_with_min_should_match():
278278 assert query .Q ("bool" , must = [qt1 , qt2 ]) == q1 & q2
279279
280280
281+ def test_negative_min_should_match ():
282+ qt1 , qt2 = query .Match (f = 1 ), query .Match (f = 2 )
283+ q1 = query .Q ("bool" , minimum_should_match = - 2 , should = [qt1 ])
284+ q2 = query .Q ("bool" , minimum_should_match = 1 , should = [qt2 ])
285+
286+ with raises (ValueError ):
287+ q1 & q2
288+ with raises (ValueError ):
289+ q2 & q1
290+
291+
292+ def test_percentage_min_should_match ():
293+ qt1 , qt2 = query .Match (f = 1 ), query .Match (f = 2 )
294+ q1 = query .Q ("bool" , minimum_should_match = "50%" , should = [qt1 ])
295+ q2 = query .Q ("bool" , minimum_should_match = 1 , should = [qt2 ])
296+
297+ with raises (ValueError ):
298+ q1 & q2
299+ with raises (ValueError ):
300+ q2 & q1
301+
302+
281303def test_inverted_query_becomes_bool_with_must_not ():
282304 q = query .Match (f = 42 )
283305
You can’t perform that action at this time.
0 commit comments