Skip to content

Commit 85dafe6

Browse files
committed
Allow index templates to have order
Fixes #1186
1 parent 49b452d commit 85dafe6

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

docs/persistence.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ Potential workflow for a set of time based indices governed by a single template
562562
return super().save(**kwargs)
563563
564564
# once, as part of application setup, during deploy/migrations:
565-
logs = Log._index.as_template('logs')
565+
logs = Log._index.as_template('logs', order=0)
566566
logs.save()
567567
568568
# to perform search across all logs:

elasticsearch_dsl/index.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
DEFAULT_DOC_TYPE = 'doc'
1010

1111
class IndexTemplate(object):
12-
def __init__(self, name, template, index=None, **kwargs):
12+
def __init__(self, name, template, index=None, order=None, **kwargs):
1313
if index is None:
1414
self._index = Index(template, **kwargs)
1515
else:
@@ -19,13 +19,16 @@ def __init__(self, name, template, index=None, **kwargs):
1919
self._index = index.clone()
2020
self._index._name = template
2121
self._template_name = name
22+
self.order = order
2223

2324
def __getattr__(self, attr_name):
2425
return getattr(self._index, attr_name)
2526

2627
def to_dict(self):
2728
d = self._index.to_dict()
2829
d['index_patterns'] = [self._index._name]
30+
if self.order is not None:
31+
d['order'] = self.order
2932
return d
3033

3134
def save(self, using=None):
@@ -51,11 +54,12 @@ def get_or_create_mapping(self):
5154
self._mapping = Mapping()
5255
return self._mapping
5356

54-
def as_template(self, template_name, pattern=None):
57+
def as_template(self, template_name, pattern=None, order=None):
5558
# TODO: should we allow pattern to be a top-level arg?
5659
# or maybe have an IndexPattern that allows for it and have
5760
# Document._index be that?
58-
return IndexTemplate(template_name, pattern or self._name, index=self)
61+
return IndexTemplate(template_name, pattern or self._name, index=self,
62+
order=order)
5963

6064
def resolve_nested(self, field_path):
6165
for doc in self._doc_types:

test_elasticsearch_dsl/test_index.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@ def test_conflicting_analyzer_raises_error():
141141

142142
with raises(ValueError):
143143
i.analyzer('my_analyzer', tokenizer='keyword', filter=['lowercase', 'stop'])
144+
145+
def test_index_template_can_have_order():
146+
i = Index('i-*')
147+
it = i.as_template('i', order=2)
148+
149+
assert {
150+
"index_patterns": ["i-*"],
151+
"order": 2
152+
} == it.to_dict()

0 commit comments

Comments
 (0)