Skip to content

Commit

Permalink
Added error checking for unsupported minimum_should_match values (#1774
Browse files Browse the repository at this point in the history
…) (#1794)

(cherry picked from commit c650b62)

Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
  • Loading branch information
github-actions[bot] and miguelgrinberg committed Apr 29, 2024
1 parent e36fb9b commit 047e5ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion elasticsearch_dsl/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ def __and__(self, other):
del q._params["minimum_should_match"]

for qx in (self, other):
# TODO: percentages will fail here
min_should_match = qx._min_should_match
# TODO: percentages or negative numbers will fail here
# for now we report an error
if not isinstance(min_should_match, int) or min_should_match < 0:
raise ValueError(
"Can only combine queries with positive integer values for minimum_should_match"
)
# all subqueries are required
if len(qx.should) <= min_should_match:
q.must.extend(qx.should)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,28 @@ def test_bool_and_bool_with_min_should_match():
assert query.Q("bool", must=[qt1, qt2]) == q1 & q2


def test_negative_min_should_match():
qt1, qt2 = query.Match(f=1), query.Match(f=2)
q1 = query.Q("bool", minimum_should_match=-2, should=[qt1])
q2 = query.Q("bool", minimum_should_match=1, should=[qt2])

with raises(ValueError):
q1 & q2
with raises(ValueError):
q2 & q1


def test_percentage_min_should_match():
qt1, qt2 = query.Match(f=1), query.Match(f=2)
q1 = query.Q("bool", minimum_should_match="50%", should=[qt1])
q2 = query.Q("bool", minimum_should_match=1, should=[qt2])

with raises(ValueError):
q1 & q2
with raises(ValueError):
q2 & q1


def test_inverted_query_becomes_bool_with_must_not():
q = query.Match(f=42)

Expand Down

0 comments on commit 047e5ea

Please sign in to comment.