From 7b141774fd4c5f8c71e3f8b6d8f745bad41f07c2 Mon Sep 17 00:00:00 2001 From: Seth Michael Larson Date: Wed, 2 Dec 2020 13:17:42 -0600 Subject: [PATCH] Inverse of an empty Bool() should be MatchNone() --- elasticsearch_dsl/query.py | 5 +++++ tests/test_query.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/elasticsearch_dsl/query.py b/elasticsearch_dsl/query.py index 48b6ba746..cedd0a7ae 100644 --- a/elasticsearch_dsl/query.py +++ b/elasticsearch_dsl/query.py @@ -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) diff --git a/tests/test_query.py b/tests/test_query.py index 098118d45..2e58040f2 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -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()