Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 8.x] Added error checking for unsupported minimum_should_match values #1794

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading