From f0cff6db3a5a2af761ee5c6f1ea00c11ee5152f3 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 25 Jul 2018 01:17:48 +0100 Subject: [PATCH] Un-deprecate index methods from Elasticsearch2SearchBackend As discussed at https://github.com/wagtail/wagtail/pull/3975#issuecomment-389961302 - the base search backend class now implements generic versions of these methods so that they can be removed from the ES2-specific code without any loss of functionality. --- wagtail/search/backends/base.py | 41 ++++++++++++++++---- wagtail/search/backends/elasticsearch2.py | 47 ----------------------- wagtail/search/tests/test_db_backend.py | 5 --- wagtail/utils/deprecation.py | 4 -- 4 files changed, 34 insertions(+), 63 deletions(-) diff --git a/wagtail/search/backends/base.py b/wagtail/search/backends/base.py index 33e270a730..8df779e8d0 100644 --- a/wagtail/search/backends/base.py +++ b/wagtail/search/backends/base.py @@ -4,7 +4,7 @@ from django.db.models.lookups import Lookup from django.db.models.query import QuerySet from django.db.models.sql.where import SubqueryConstraint, WhereNode -from wagtail.search.index import class_is_indexed +from wagtail.search.index import class_is_indexed, get_indexed_models from wagtail.search.query import MATCH_ALL, PlainText @@ -272,6 +272,28 @@ class EmptySearchResults(BaseSearchResults): return 0 +class NullIndex: + """ + Index class that provides do-nothing implementations of the indexing operations required by + BaseSearchBackend. Use this for search backends that do not maintain an index, such as the + database backend. + """ + def add_model(self, model): + pass + + def refresh(self): + pass + + def add_item(self, item): + pass + + def add_items(self, model, items): + pass + + def delete_item(self, item): + pass + + class BaseSearchBackend: query_compiler_class = None autocomplete_query_compiler_class = None @@ -282,7 +304,7 @@ class BaseSearchBackend: pass def get_index_for_model(self, model): - return None + return NullIndex() def get_rebuilder(self): return None @@ -291,19 +313,24 @@ class BaseSearchBackend: raise NotImplementedError def add_type(self, model): - raise NotImplementedError + self.get_index_for_model(model).add_model(model) def refresh_index(self): - raise NotImplementedError + refreshed_indexes = [] + for model in get_indexed_models: + index = self.get_index_for_model(model) + if index not in refreshed_indexes: + index.refresh() + refreshed_indexes.append(index) def add(self, obj): - raise NotImplementedError + self.get_index_for_model(type(obj)).add_item(obj) def add_bulk(self, model, obj_list): - raise NotImplementedError + self.get_index_for_model(model).add_items(model, obj_list) def delete(self, obj): - raise NotImplementedError + self.get_index_for_model(type(obj)).delete_item(obj) def _search(self, query_compiler_class, query, model_or_queryset, **kwargs): # Find model/queryset diff --git a/wagtail/search/backends/elasticsearch2.py b/wagtail/search/backends/elasticsearch2.py index 8ff98e5db1..4341156cf2 100644 --- a/wagtail/search/backends/elasticsearch2.py +++ b/wagtail/search/backends/elasticsearch2.py @@ -1,6 +1,5 @@ import copy import json -import warnings from collections import OrderedDict from urllib.parse import urlparse @@ -16,7 +15,6 @@ from wagtail.search.backends.base import ( from wagtail.search.index import ( AutocompleteField, FilterField, Indexed, RelatedFields, SearchField, class_is_indexed) from wagtail.search.query import And, Boost, MatchAll, Not, Or, PlainText -from wagtail.utils.deprecation import RemovedInWagtail22Warning from wagtail.utils.utils import deep_update @@ -1100,50 +1098,5 @@ class Elasticsearch2SearchBackend(BaseSearchBackend): # Use the rebuilder to reset the index self.get_rebuilder().reset_index() - def add_type(self, model): - warnings.warn( - "The `backend.add_type(model)` method is deprecated. " - "Please use `backend.get_index_for_model(model).add_model(model)` instead.", - category=RemovedInWagtail22Warning - ) - - self.get_index_for_model(model).add_model(model) - - def refresh_index(self): - warnings.warn( - "The `backend.refresh_index()` method is deprecated. " - "Please use `backend.get_index_for_model(model).refresh()` for each model instead.", - category=RemovedInWagtail22Warning - ) - - self.get_index().refresh() - - def add(self, obj): - warnings.warn( - "The `backend.add(obj)` method is deprecated. " - "Please use `backend.get_index_for_model(type(obj)).add_item(obj)` instead.", - category=RemovedInWagtail22Warning - ) - - self.get_index_for_model(type(obj)).add_item(obj) - - def add_bulk(self, model, obj_list): - warnings.warn( - "The `backend.add_bulk(model, obj_list)` method is deprecated. " - "Please use `self.get_index_for_model(model).add_items(model, obj_list)` instead.", - category=RemovedInWagtail22Warning - ) - - self.get_index_for_model(model).add_items(model, obj_list) - - def delete(self, obj): - warnings.warn( - "The `backend.delete(obj)` method is deprecated. " - "Please use `backend.get_index_for_model(type(obj)).delete_item(obj)` instead.", - category=RemovedInWagtail22Warning - ) - - self.get_index_for_model(type(obj)).delete_item(obj) - SearchBackend = Elasticsearch2SearchBackend diff --git a/wagtail/search/tests/test_db_backend.py b/wagtail/search/tests/test_db_backend.py index a5014699db..e03cf29c26 100644 --- a/wagtail/search/tests/test_db_backend.py +++ b/wagtail/search/tests/test_db_backend.py @@ -33,11 +33,6 @@ class TestDBBackend(BackendTests, TestCase): def test_search_boosting_on_related_fields(self): super().test_search_boosting_on_related_fields() - # Doesn't support ranking - @unittest.expectedFailure - def test_same_rank_pages(self): - super(TestDBBackend, self).test_same_rank_pages() - # Doesn't support searching specific fields @unittest.expectedFailure def test_search_child_class_field_from_parent(self): diff --git a/wagtail/utils/deprecation.py b/wagtail/utils/deprecation.py index d76077b39f..f983e3cea7 100644 --- a/wagtail/utils/deprecation.py +++ b/wagtail/utils/deprecation.py @@ -1,7 +1,3 @@ -class RemovedInWagtail22Warning(DeprecationWarning): - pass - - class RemovedInWagtail23Warning(DeprecationWarning): pass