@@ -73,6 +73,47 @@ async def save(
73
73
)
74
74
75
75
76
+ class AsyncNewIndexTemplate :
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
+
76
117
class AsyncIndex (IndexBase ):
77
118
_using : AsyncUsingType
78
119
@@ -109,6 +150,19 @@ def as_template(
109
150
template_name , pattern or self ._name , index = self , order = order
110
151
)
111
152
153
+ def as_new_template (
154
+ self ,
155
+ template_name : str ,
156
+ pattern : Optional [str ] = None ,
157
+ priority : Optional [int ] = None ,
158
+ ) -> AsyncNewIndexTemplate :
159
+ # TODO: should we allow pattern to be a top-level arg?
160
+ # or maybe have an IndexPattern that allows for it and have
161
+ # Document._index be that?
162
+ return AsyncNewIndexTemplate (
163
+ template_name , pattern or self ._name , index = self , priority = priority
164
+ )
165
+
112
166
async def load_mappings (self , using : Optional [AsyncUsingType ] = None ) -> None :
113
167
await self .get_or_create_mapping ().update_from_es (
114
168
self ._name , using = using or self ._using
0 commit comments