|
29 | 29 | Q, |
30 | 30 | Search, |
31 | 31 | ) |
| 32 | +from opensearchpy.connection.connections import ( |
| 33 | + get_connection, |
| 34 | +) |
32 | 35 | from opensearchpy.helpers.aggs import ( |
33 | 36 | Agg, |
34 | 37 | Terms, |
@@ -664,6 +667,28 @@ def page_link(*, previous): |
664 | 667 | order=pagination.order) |
665 | 668 |
|
666 | 669 |
|
| 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 | + |
667 | 692 | class ElasticsearchService(DocumentService): |
668 | 693 |
|
669 | 694 | @cached_property |
@@ -701,12 +726,12 @@ def create_request(self, |
701 | 726 | catalog: CatalogName, |
702 | 727 | entity_type: str, |
703 | 728 | doc_type: DocumentType = DocumentType.aggregate |
704 | | - ) -> Search: |
| 729 | + ) -> TemplateSearch: |
705 | 730 | """ |
706 | 731 | Create an Elasticsearch request against the index containing documents |
707 | 732 | of the given entity and document types, in the given catalog. |
708 | 733 | """ |
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))) |
0 commit comments