@@ -73,6 +73,47 @@ async def save(
7373 )
7474
7575
76+ class AsyncComposableIndexTemplate :
77+ def __init__ (
78+ self ,
79+ name : str ,
80+ template : str ,
81+ index : Optional ["AsyncIndex" ] = None ,
82+ priority : Optional [int ] = None ,
83+ ** kwargs : Any ,
84+ ):
85+ if index is None :
86+ self ._index = AsyncIndex (template , ** kwargs )
87+ else :
88+ if kwargs :
89+ raise ValueError (
90+ "You cannot specify options for Index when"
91+ " passing an Index instance."
92+ )
93+ self ._index = index .clone ()
94+ self ._index ._name = template
95+ self ._template_name = name
96+ self .priority = priority
97+
98+ def __getattr__ (self , attr_name : str ) -> Any :
99+ return getattr (self ._index , attr_name )
100+
101+ def to_dict (self ) -> Dict [str , Any ]:
102+ d : Dict [str , Any ] = {"template" : self ._index .to_dict ()}
103+ d ["index_patterns" ] = [self ._index ._name ]
104+ if self .priority is not None :
105+ d ["priority" ] = self .priority
106+ return d
107+
108+ async def save (
109+ self , using : Optional [AsyncUsingType ] = None
110+ ) -> "ObjectApiResponse[Any]" :
111+ es = get_connection (using or self ._index ._using )
112+ return await es .indices .put_index_template (
113+ name = self ._template_name , ** self .to_dict ()
114+ )
115+
116+
76117class AsyncIndex (IndexBase ):
77118 _using : AsyncUsingType
78119
@@ -102,13 +143,20 @@ def as_template(
102143 pattern : Optional [str ] = None ,
103144 order : Optional [int ] = None ,
104145 ) -> AsyncIndexTemplate :
105- # TODO: should we allow pattern to be a top-level arg?
106- # or maybe have an IndexPattern that allows for it and have
107- # Document._index be that?
108146 return AsyncIndexTemplate (
109147 template_name , pattern or self ._name , index = self , order = order
110148 )
111149
150+ def as_composable_template (
151+ self ,
152+ template_name : str ,
153+ pattern : Optional [str ] = None ,
154+ priority : Optional [int ] = None ,
155+ ) -> AsyncComposableIndexTemplate :
156+ return AsyncComposableIndexTemplate (
157+ template_name , pattern or self ._name , index = self , priority = priority
158+ )
159+
112160 async def load_mappings (self , using : Optional [AsyncUsingType ] = None ) -> None :
113161 await self .get_or_create_mapping ().update_from_es (
114162 self ._name , using = using or self ._using
0 commit comments