Skip to content

Commit baaf601

Browse files
support composable templates
1 parent 5511abe commit baaf601

File tree

6 files changed

+110
-4
lines changed

6 files changed

+110
-4
lines changed

elasticsearch_dsl/_async/index.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,47 @@ async def save(
7373
)
7474

7575

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+
76117
class AsyncIndex(IndexBase):
77118
_using: AsyncUsingType
78119

@@ -109,6 +150,19 @@ def as_template(
109150
template_name, pattern or self._name, index=self, order=order
110151
)
111152

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+
112166
async def load_mappings(self, using: Optional[AsyncUsingType] = None) -> None:
113167
await self.get_or_create_mapping().update_from_es(
114168
self._name, using=using or self._using

elasticsearch_dsl/_sync/index.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,43 @@ def save(self, using: Optional[UsingType] = None) -> "ObjectApiResponse[Any]":
6969
return es.indices.put_template(name=self._template_name, body=self.to_dict())
7070

7171

72+
class SyncNewIndexTemplate:
73+
def __init__(
74+
self,
75+
name: str,
76+
template: str,
77+
index: Optional["Index"] = None,
78+
priority: Optional[int] = None,
79+
**kwargs: Any,
80+
):
81+
if index is None:
82+
self._index = Index(template, **kwargs)
83+
else:
84+
if kwargs:
85+
raise ValueError(
86+
"You cannot specify options for Index when"
87+
" passing an Index instance."
88+
)
89+
self._index = index.clone()
90+
self._index._name = template
91+
self._template_name = name
92+
self.priority = priority
93+
94+
def __getattr__(self, attr_name: str) -> Any:
95+
return getattr(self._index, attr_name)
96+
97+
def to_dict(self) -> Dict[str, Any]:
98+
d: Dict[str, Any] = {"template": self._index.to_dict()}
99+
d["index_patterns"] = [self._index._name]
100+
if self.priority is not None:
101+
d["priority"] = self.priority
102+
return d
103+
104+
def save(self, using: Optional[UsingType] = None) -> "ObjectApiResponse[Any]":
105+
es = get_connection(using or self._index._using)
106+
return es.indices.put_index_template(name=self._template_name, **self.to_dict())
107+
108+
72109
class Index(IndexBase):
73110
_using: UsingType
74111

@@ -103,6 +140,19 @@ def as_template(
103140
template_name, pattern or self._name, index=self, order=order
104141
)
105142

143+
def as_new_template(
144+
self,
145+
template_name: str,
146+
pattern: Optional[str] = None,
147+
priority: Optional[int] = None,
148+
) -> SyncNewIndexTemplate:
149+
# TODO: should we allow pattern to be a top-level arg?
150+
# or maybe have an IndexPattern that allows for it and have
151+
# Document._index be that?
152+
return SyncNewIndexTemplate(
153+
template_name, pattern or self._name, index=self, priority=priority
154+
)
155+
106156
def load_mappings(self, using: Optional[UsingType] = None) -> None:
107157
self.get_or_create_mapping().update_from_es(
108158
self._name, using=using or self._using

examples/alias_migration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
ALIAS = "test-blog"
4646
PATTERN = ALIAS + "-*"
47+
PRIORITY = 100
4748

4849

4950
class BlogPost(Document):
@@ -81,7 +82,7 @@ def setup() -> None:
8182
deploy.
8283
"""
8384
# create an index template
84-
index_template = BlogPost._index.as_template(ALIAS, PATTERN)
85+
index_template = BlogPost._index.as_new_template(ALIAS, PATTERN, priority=PRIORITY)
8586
# upload the template into elasticsearch
8687
# potentially overriding the one already there
8788
index_template.save()

examples/async/alias_migration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
ALIAS = "test-blog"
4747
PATTERN = ALIAS + "-*"
48+
PRIORITY = 100
4849

4950

5051
class BlogPost(AsyncDocument):
@@ -82,7 +83,7 @@ async def setup() -> None:
8283
deploy.
8384
"""
8485
# create an index template
85-
index_template = BlogPost._index.as_template(ALIAS, PATTERN)
86+
index_template = BlogPost._index.as_new_template(ALIAS, PATTERN, priority=PRIORITY)
8687
# upload the template into elasticsearch
8788
# potentially overriding the one already there
8889
await index_template.save()

examples/async/parent_child.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ async def save(self, **kwargs: Any) -> None: # type: ignore[override]
226226

227227
async def setup() -> None:
228228
"""Create an IndexTemplate and save it into elasticsearch."""
229-
index_template = Post._index.as_template("base")
229+
index_template = Post._index.as_new_template("base", priority=100)
230230
await index_template.save()
231231

232232

examples/parent_child.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def save(self, **kwargs: Any) -> None: # type: ignore[override]
225225

226226
def setup() -> None:
227227
"""Create an IndexTemplate and save it into elasticsearch."""
228-
index_template = Post._index.as_template("base")
228+
index_template = Post._index.as_new_template("base", priority=100)
229229
index_template.save()
230230

231231

0 commit comments

Comments
 (0)