From f2ad4cea3c8a6fb3f06aa8c8326a6b1e18f26477 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Fri, 20 Oct 2017 10:40:00 +0100 Subject: [PATCH] Fix __isnull=True lookup for Elasticsearch 5 --- wagtail/search/backends/elasticsearch5.py | 21 +++++++++++++++++++ .../tests/test_elasticsearch5_backend.py | 9 ++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/wagtail/search/backends/elasticsearch5.py b/wagtail/search/backends/elasticsearch5.py index d8bf370b7..0c43b16b3 100644 --- a/wagtail/search/backends/elasticsearch5.py +++ b/wagtail/search/backends/elasticsearch5.py @@ -16,6 +16,27 @@ class Elasticsearch5Index(Elasticsearch2Index): class Elasticsearch5SearchQuery(Elasticsearch2SearchQuery): mapping_class = Elasticsearch5Mapping + def _process_lookup(self, field, lookup, value): + column_name = self.mapping.get_field_column_name(field) + + if lookup == 'isnull': + query = { + 'exists': { + 'field': column_name, + } + } + + if value: + query = { + 'bool': { + 'mustNot': query + } + } + + return query + + return super(Elasticsearch5SearchQuery, self)._process_lookup(field, lookup, value) + def _connect_filters(self, filters, connector, negated): if filters: if len(filters) == 1: diff --git a/wagtail/search/tests/test_elasticsearch5_backend.py b/wagtail/search/tests/test_elasticsearch5_backend.py index 228cd85a3..205f50d13 100644 --- a/wagtail/search/tests/test_elasticsearch5_backend.py +++ b/wagtail/search/tests/test_elasticsearch5_backend.py @@ -18,11 +18,6 @@ from .test_backends import BackendTests class TestElasticsearch5SearchBackend(BackendTests, ElasticsearchCommonSearchBackendTests, TestCase): backend_path = 'wagtail.search.backends.elasticsearch5' - # Broken - @unittest.expectedFailure - def test_filter_isnull_true(self): - super(TestElasticsearch5SearchBackend, self).test_filter_isnull_true() - # Broken @unittest.expectedFailure def test_delete(self): @@ -190,7 +185,7 @@ class TestElasticsearch5SearchQuery(TestCase): # Check it expected_result = {'bool': {'filter': [ {'match': {'content_type': 'searchtests.Book'}}, - {'missing': {'field': 'title_filter'}} + {'bool': {'mustNot': {'exists': {'field': 'title_filter'}}}} ], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} self.assertDictEqual(query.get_query(), expected_result) @@ -201,7 +196,7 @@ class TestElasticsearch5SearchQuery(TestCase): # Check it expected_result = {'bool': {'filter': [ {'match': {'content_type': 'searchtests.Book'}}, - {'missing': {'field': 'title_filter'}} + {'bool': {'mustNot': {'exists': {'field': 'title_filter'}}}} ], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}} self.assertDictEqual(query.get_query(), expected_result)