kopia lustrzana https://github.com/wagtail/wagtail
Fix Elasticsearch7 backend to work on pre-7.15 library versions
Fixes #7920. Check library version and only patch backend classes with the updated API calls if it's >=7.15.stable/5.1.x
rodzic
67c61aa334
commit
80cbc42497
|
@ -1,5 +1,6 @@
|
|||
from copy import deepcopy
|
||||
|
||||
from elasticsearch import VERSION as ELASTICSEARCH_VERSION
|
||||
from elasticsearch import NotFoundError
|
||||
from elasticsearch.helpers import bulk
|
||||
|
||||
|
@ -13,6 +14,8 @@ from wagtail.search.backends.elasticsearch6 import (
|
|||
)
|
||||
from wagtail.search.index import class_is_indexed
|
||||
|
||||
use_new_elasticsearch_api = ELASTICSEARCH_VERSION >= (7, 15)
|
||||
|
||||
|
||||
class Elasticsearch7Mapping(Elasticsearch6Mapping):
|
||||
def get_mapping(self):
|
||||
|
@ -21,17 +24,19 @@ class Elasticsearch7Mapping(Elasticsearch6Mapping):
|
|||
|
||||
|
||||
class Elasticsearch7Index(Elasticsearch6Index):
|
||||
def put(self):
|
||||
self.es.indices.create(index=self.name, **self.backend.settings)
|
||||
if use_new_elasticsearch_api:
|
||||
|
||||
def delete(self):
|
||||
try:
|
||||
self.es.indices.delete(index=self.name)
|
||||
except NotFoundError:
|
||||
pass
|
||||
def put(self):
|
||||
self.es.indices.create(index=self.name, **self.backend.settings)
|
||||
|
||||
def refresh(self):
|
||||
self.es.indices.refresh(index=self.name)
|
||||
def delete(self):
|
||||
try:
|
||||
self.es.indices.delete(index=self.name)
|
||||
except NotFoundError:
|
||||
pass
|
||||
|
||||
def refresh(self):
|
||||
self.es.indices.refresh(index=self.name)
|
||||
|
||||
def add_model(self, model):
|
||||
# Get mapping
|
||||
|
@ -40,20 +45,36 @@ class Elasticsearch7Index(Elasticsearch6Index):
|
|||
# Put mapping
|
||||
self.es.indices.put_mapping(index=self.name, body=mapping.get_mapping())
|
||||
|
||||
def add_item(self, item):
|
||||
# Make sure the object can be indexed
|
||||
if not class_is_indexed(item.__class__):
|
||||
return
|
||||
if use_new_elasticsearch_api:
|
||||
|
||||
# Get mapping
|
||||
mapping = self.mapping_class(item.__class__)
|
||||
def add_item(self, item):
|
||||
# Make sure the object can be indexed
|
||||
if not class_is_indexed(item.__class__):
|
||||
return
|
||||
|
||||
# Add document to index
|
||||
self.es.index(
|
||||
index=self.name,
|
||||
document=mapping.get_document(item),
|
||||
id=mapping.get_document_id(item),
|
||||
)
|
||||
# Get mapping
|
||||
mapping = self.mapping_class(item.__class__)
|
||||
|
||||
# Add document to index
|
||||
self.es.index(
|
||||
index=self.name,
|
||||
document=mapping.get_document(item),
|
||||
id=mapping.get_document_id(item),
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
def add_item(self, item):
|
||||
# Make sure the object can be indexed
|
||||
if not class_is_indexed(item.__class__):
|
||||
return
|
||||
# Get mapping
|
||||
mapping = self.mapping_class(item.__class__)
|
||||
|
||||
# Add document to index
|
||||
self.es.index(
|
||||
self.name, mapping.get_document(item), id=mapping.get_document_id(item)
|
||||
)
|
||||
|
||||
def add_items(self, model, items):
|
||||
if not class_is_indexed(model):
|
||||
|
@ -93,10 +114,12 @@ class Elasticsearch7SearchQueryCompiler(Elasticsearch6SearchQueryCompiler):
|
|||
|
||||
|
||||
class Elasticsearch7SearchResults(Elasticsearch6SearchResults):
|
||||
def _backend_do_search(self, body, **kwargs):
|
||||
# As of Elasticsearch 7, the 'body' parameter is deprecated; instead, the top-level
|
||||
# keys of the body dict are now kwargs in their own right
|
||||
return self.backend.es.search(**body, **kwargs)
|
||||
if use_new_elasticsearch_api:
|
||||
|
||||
def _backend_do_search(self, body, **kwargs):
|
||||
# As of Elasticsearch 7.15, the 'body' parameter is deprecated; instead, the top-level
|
||||
# keys of the body dict are now kwargs in their own right
|
||||
return self.backend.es.search(**body, **kwargs)
|
||||
|
||||
|
||||
class Elasticsearch7AutocompleteQueryCompiler(
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.core.exceptions import ImproperlyConfigured
|
||||
from elasticsearch import NotFoundError
|
||||
|
||||
from wagtail.search.backends.elasticsearch7 import (
|
||||
Elasticsearch7AutocompleteQueryCompiler,
|
||||
|
@ -8,6 +9,7 @@ from wagtail.search.backends.elasticsearch7 import (
|
|||
Elasticsearch7SearchQueryCompiler,
|
||||
Elasticsearch7SearchResults,
|
||||
)
|
||||
from wagtail.search.index import class_is_indexed
|
||||
|
||||
|
||||
class Elasticsearch8Mapping(Elasticsearch7Mapping):
|
||||
|
@ -15,6 +17,18 @@ class Elasticsearch8Mapping(Elasticsearch7Mapping):
|
|||
|
||||
|
||||
class Elasticsearch8Index(Elasticsearch7Index):
|
||||
def put(self):
|
||||
self.es.indices.create(index=self.name, **self.backend.settings)
|
||||
|
||||
def delete(self):
|
||||
try:
|
||||
self.es.indices.delete(index=self.name)
|
||||
except NotFoundError:
|
||||
pass
|
||||
|
||||
def refresh(self):
|
||||
self.es.indices.refresh(index=self.name)
|
||||
|
||||
def add_model(self, model):
|
||||
# Get mapping
|
||||
mapping = self.mapping_class(model)
|
||||
|
@ -22,13 +36,31 @@ class Elasticsearch8Index(Elasticsearch7Index):
|
|||
# Put mapping
|
||||
self.es.indices.put_mapping(index=self.name, **mapping.get_mapping())
|
||||
|
||||
def add_item(self, item):
|
||||
# Make sure the object can be indexed
|
||||
if not class_is_indexed(item.__class__):
|
||||
return
|
||||
|
||||
# Get mapping
|
||||
mapping = self.mapping_class(item.__class__)
|
||||
|
||||
# Add document to index
|
||||
self.es.index(
|
||||
index=self.name,
|
||||
document=mapping.get_document(item),
|
||||
id=mapping.get_document_id(item),
|
||||
)
|
||||
|
||||
|
||||
class Elasticsearch8SearchQueryCompiler(Elasticsearch7SearchQueryCompiler):
|
||||
mapping_class = Elasticsearch8Mapping
|
||||
|
||||
|
||||
class Elasticsearch8SearchResults(Elasticsearch7SearchResults):
|
||||
pass
|
||||
def _backend_do_search(self, body, **kwargs):
|
||||
# As of Elasticsearch 7.15, the 'body' parameter is deprecated; instead, the top-level
|
||||
# keys of the body dict are now kwargs in their own right
|
||||
return self.backend.es.search(**body, **kwargs)
|
||||
|
||||
|
||||
class Elasticsearch8AutocompleteQueryCompiler(Elasticsearch7AutocompleteQueryCompiler):
|
||||
|
|
|
@ -20,6 +20,16 @@ except ImportError:
|
|||
ELASTICSEARCH_VERSION = (0, 0, 0)
|
||||
|
||||
|
||||
use_new_elasticsearch_api = ELASTICSEARCH_VERSION >= (7, 15)
|
||||
|
||||
if use_new_elasticsearch_api:
|
||||
search_query_kwargs = {
|
||||
"query": "QUERY",
|
||||
}
|
||||
else:
|
||||
search_query_kwargs = {"body": {"query": "QUERY"}}
|
||||
|
||||
|
||||
@unittest.skipIf(ELASTICSEARCH_VERSION[0] != 7, "Elasticsearch 7 required")
|
||||
class TestElasticsearch7SearchBackend(ElasticsearchCommonSearchBackendTests, TestCase):
|
||||
backend_path = "wagtail.search.backends.elasticsearch7"
|
||||
|
@ -915,12 +925,12 @@ class TestElasticsearch7SearchResults(TestCase):
|
|||
list(results) # Performs search
|
||||
|
||||
search.assert_any_call(
|
||||
query="QUERY",
|
||||
_source=False,
|
||||
stored_fields="pk",
|
||||
index="wagtail__searchtests_book",
|
||||
scroll="2m",
|
||||
size=100,
|
||||
**search_query_kwargs,
|
||||
)
|
||||
|
||||
@mock.patch("elasticsearch.Elasticsearch.search")
|
||||
|
@ -933,11 +943,11 @@ class TestElasticsearch7SearchResults(TestCase):
|
|||
|
||||
search.assert_any_call(
|
||||
from_=10,
|
||||
query="QUERY",
|
||||
_source=False,
|
||||
stored_fields="pk",
|
||||
index="wagtail__searchtests_book",
|
||||
size=1,
|
||||
**search_query_kwargs,
|
||||
)
|
||||
|
||||
@mock.patch("elasticsearch.Elasticsearch.search")
|
||||
|
@ -949,11 +959,11 @@ class TestElasticsearch7SearchResults(TestCase):
|
|||
|
||||
search.assert_any_call(
|
||||
from_=1,
|
||||
query="QUERY",
|
||||
_source=False,
|
||||
stored_fields="pk",
|
||||
index="wagtail__searchtests_book",
|
||||
size=3,
|
||||
**search_query_kwargs,
|
||||
)
|
||||
|
||||
@mock.patch("elasticsearch.Elasticsearch.search")
|
||||
|
@ -965,11 +975,11 @@ class TestElasticsearch7SearchResults(TestCase):
|
|||
|
||||
search.assert_any_call(
|
||||
from_=10,
|
||||
query="QUERY",
|
||||
_source=False,
|
||||
stored_fields="pk",
|
||||
index="wagtail__searchtests_book",
|
||||
size=10,
|
||||
**search_query_kwargs,
|
||||
)
|
||||
|
||||
@mock.patch("elasticsearch.Elasticsearch.search")
|
||||
|
@ -982,11 +992,11 @@ class TestElasticsearch7SearchResults(TestCase):
|
|||
|
||||
search.assert_any_call(
|
||||
from_=20,
|
||||
query="QUERY",
|
||||
_source=False,
|
||||
stored_fields="pk",
|
||||
index="wagtail__searchtests_book",
|
||||
size=1,
|
||||
**search_query_kwargs,
|
||||
)
|
||||
|
||||
@mock.patch("elasticsearch.Elasticsearch.search")
|
||||
|
|
Ładowanie…
Reference in New Issue