Skip to content

Commit 0018866

Browse files
committed
ES service uses template_search instead of search
1 parent 1257686 commit 0018866

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

src/azul/service/elasticsearch_service.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
Q,
3030
Search,
3131
)
32+
from opensearchpy.connection.connections import (
33+
get_connection,
34+
)
3235
from opensearchpy.helpers.aggs import (
3336
Agg,
3437
Terms,
@@ -664,6 +667,28 @@ def page_link(*, previous):
664667
order=pagination.order)
665668

666669

670+
class TemplateSearch(Search):
671+
672+
def __init__(self, **kwargs: Any):
673+
super().__init__(**kwargs)
674+
self._template_params = {}
675+
676+
def execute(self, ignore_cache: bool = False) -> Any:
677+
if ignore_cache or not hasattr(self, '_response'):
678+
opensearch = get_connection(self._using)
679+
body = {
680+
'source': json.dumps(self.to_dict()),
681+
'params': self._template_params,
682+
}
683+
self._response = self._response_class(
684+
self,
685+
opensearch.search_template(
686+
index=self._index, body=body, **self._params
687+
),
688+
)
689+
return self._response
690+
691+
667692
class ElasticsearchService(DocumentService):
668693

669694
@cached_property
@@ -701,12 +726,12 @@ def create_request(self,
701726
catalog: CatalogName,
702727
entity_type: str,
703728
doc_type: DocumentType = DocumentType.aggregate
704-
) -> Search:
729+
) -> TemplateSearch:
705730
"""
706731
Create an Elasticsearch request against the index containing documents
707732
of the given entity and document types, in the given catalog.
708733
"""
709-
return Search(using=self._es_client,
710-
index=str(IndexName.create(catalog=catalog,
711-
qualifier=entity_type,
712-
doc_type=doc_type)))
734+
return TemplateSearch(using=self._es_client,
735+
index=str(IndexName.create(catalog=catalog,
736+
qualifier=entity_type,
737+
doc_type=doc_type)))

test/es_test_case.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,17 @@ def setUpClass(cls):
7272
# indices in a single request. Speeds up deletion of indices
7373
# between tests.
7474
#
75-
'action.destructive_requires_name': False
75+
'action.destructive_requires_name': False,
76+
77+
# The service uses template queries when reading from the
78+
# index which, during tests, can far exceed the default
79+
# script compilation rate of 75/5m. Rendering a template
80+
# query using mustache is a very cheap operation compared to
81+
# other contexts (e.g. generating bytecode for a painless
82+
# script), so performance shouldn't be significantly
83+
# affected.
84+
#
85+
'script.context.template.max_compilations_rate': 'unlimited'
7686
}
7787
})
7888
except BaseException: # no coverage

0 commit comments

Comments
 (0)