File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -208,8 +208,13 @@ def __and__(self, other):
208
208
del q ._params ["minimum_should_match" ]
209
209
210
210
for qx in (self , other ):
211
- # TODO: percentages will fail here
212
211
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 (
216
+ "Can only combine queries with positive integer values for minimum_should_match"
217
+ )
213
218
# all subqueries are required
214
219
if len (qx .should ) <= min_should_match :
215
220
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():
278
278
assert query .Q ("bool" , must = [qt1 , qt2 ]) == q1 & q2
279
279
280
280
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
+
281
303
def test_inverted_query_becomes_bool_with_must_not ():
282
304
q = query .Match (f = 42 )
283
305
You can’t perform that action at this time.
0 commit comments