Skip to content

Inverse of an empty Bool() should be MatchNone() #1459

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

Merged
merged 2 commits into from
Dec 2, 2020
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
5 changes: 5 additions & 0 deletions elasticsearch_dsl/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ def _min_should_match(self):
)

def __invert__(self):
# Because an empty Bool query is treated like
# MatchAll the inverse should be MatchNone
if not any(chain(self.must, self.filter, self.should, self.must_not)):
return MatchNone()

negations = []
for q in chain(self.must, self.filter):
negations.append(~q)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ def test_not_match_none_is_match_all():
assert ~q == query.MatchAll()


def test_invert_empty_bool_is_match_none():
q = query.Bool()

assert ~q == query.MatchNone()


def test_match_none_or_query_equals_query():
q1 = query.Match(f=42)
q2 = query.MatchNone()
Expand Down