Skip to content

Commit c650b62

Browse files
Added error checking for unsupported minimum_should_match values (#1774)
1 parent 5a6a231 commit c650b62

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

elasticsearch_dsl/query.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,13 @@ 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(
216+
"Can only combine queries with positive integer values for minimum_should_match"
217+
)
213218
# all subqueries are required
214219
if len(qx.should) <= min_should_match:
215220
q.must.extend(qx.should)

tests/test_query.py

+22
Original file line numberDiff line numberDiff 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+
281303
def test_inverted_query_becomes_bool_with_must_not():
282304
q = query.Match(f=42)
283305

0 commit comments

Comments
 (0)