Address deprecation warnings raised by elasticsearch-py 7

* pass all arguments as keyword args rather than positional
* instead of passing a 'body' argument to search, pass its top-level keys as kwargs
pull/10477/head
Matt Westcott 2023-07-13 19:41:21 +01:00
rodzic 5e1b12ce32
commit 9203716bae
3 zmienionych plików z 35 dodań i 13 usunięć

Wyświetl plik

@ -691,11 +691,11 @@ class Elasticsearch5SearchResults(BaseSearchResults):
}
# Send to Elasticsearch
response = self.backend.es.search(
response = self._backend_do_search(
body,
index=self.backend.get_index_for_model(
self.query_compiler.queryset.model
).name,
body=body,
size=0,
)
@ -741,6 +741,11 @@ class Elasticsearch5SearchResults(BaseSearchResults):
if result:
yield result
def _backend_do_search(self, body, **kwargs):
# Send the search query to the backend. Wrapped here so that it can be overridden
# to handle different calling conventions for the 'body' parameter
return self.backend.es.search(body=body, **kwargs)
def _do_search(self):
PAGE_SIZE = 100
@ -751,11 +756,11 @@ class Elasticsearch5SearchResults(BaseSearchResults):
use_scroll = limit is None or limit > PAGE_SIZE
body = self._get_es_body()
params = {
"index": self.backend.get_index_for_model(
self.query_compiler.queryset.model
).name,
"body": self._get_es_body(),
"_source": False,
self.fields_param_name: "pk",
}
@ -772,7 +777,7 @@ class Elasticsearch5SearchResults(BaseSearchResults):
skip = self.start
# Send to Elasticsearch
page = self.backend.es.search(**params)
page = self._backend_do_search(body, **params)
while True:
hits = page["hits"]["hits"]
@ -818,7 +823,7 @@ class Elasticsearch5SearchResults(BaseSearchResults):
)
# Send to Elasticsearch
hits = self.backend.es.search(**params)["hits"]["hits"]
hits = self._backend_do_search(body, **params)["hits"]["hits"]
# Get results
for result in self._get_results_from_hits(hits):

Wyświetl plik

@ -21,6 +21,18 @@ class Elasticsearch7Mapping(Elasticsearch6Mapping):
class Elasticsearch7Index(Elasticsearch6Index):
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)
@ -38,7 +50,9 @@ class Elasticsearch7Index(Elasticsearch6Index):
# Add document to index
self.es.index(
self.name, mapping.get_document(item), id=mapping.get_document_id(item)
index=self.name,
document=mapping.get_document(item),
id=mapping.get_document_id(item),
)
def add_items(self, model, items):
@ -69,7 +83,7 @@ class Elasticsearch7Index(Elasticsearch6Index):
# Delete document
try:
self.es.delete(self.name, mapping.get_document_id(item))
self.es.delete(index=self.name, id=mapping.get_document_id(item))
except NotFoundError:
pass # Document doesn't exist, ignore this exception
@ -79,7 +93,10 @@ class Elasticsearch7SearchQueryCompiler(Elasticsearch6SearchQueryCompiler):
class Elasticsearch7SearchResults(Elasticsearch6SearchResults):
pass
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)
class Elasticsearch7AutocompleteQueryCompiler(

Wyświetl plik

@ -902,7 +902,7 @@ class TestElasticsearch7SearchResults(TestCase):
list(results) # Performs search
search.assert_any_call(
body={"query": "QUERY"},
query="QUERY",
_source=False,
stored_fields="pk",
index="wagtail__searchtests_book",
@ -920,7 +920,7 @@ class TestElasticsearch7SearchResults(TestCase):
search.assert_any_call(
from_=10,
body={"query": "QUERY"},
query="QUERY",
_source=False,
stored_fields="pk",
index="wagtail__searchtests_book",
@ -936,7 +936,7 @@ class TestElasticsearch7SearchResults(TestCase):
search.assert_any_call(
from_=1,
body={"query": "QUERY"},
query="QUERY",
_source=False,
stored_fields="pk",
index="wagtail__searchtests_book",
@ -952,7 +952,7 @@ class TestElasticsearch7SearchResults(TestCase):
search.assert_any_call(
from_=10,
body={"query": "QUERY"},
query="QUERY",
_source=False,
stored_fields="pk",
index="wagtail__searchtests_book",
@ -969,7 +969,7 @@ class TestElasticsearch7SearchResults(TestCase):
search.assert_any_call(
from_=20,
body={"query": "QUERY"},
query="QUERY",
_source=False,
stored_fields="pk",
index="wagtail__searchtests_book",