Skip to content

Commit

Permalink
Allow index templates to have order
Browse files Browse the repository at this point in the history
Fixes #1186
  • Loading branch information
honzakral committed Jun 1, 2019
1 parent 49b452d commit 85dafe6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ Potential workflow for a set of time based indices governed by a single template
return super().save(**kwargs)
# once, as part of application setup, during deploy/migrations:
logs = Log._index.as_template('logs')
logs = Log._index.as_template('logs', order=0)
logs.save()
# to perform search across all logs:
Expand Down
10 changes: 7 additions & 3 deletions elasticsearch_dsl/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
DEFAULT_DOC_TYPE = 'doc'

class IndexTemplate(object):
def __init__(self, name, template, index=None, **kwargs):
def __init__(self, name, template, index=None, order=None, **kwargs):
if index is None:
self._index = Index(template, **kwargs)
else:
Expand All @@ -19,13 +19,16 @@ def __init__(self, name, template, index=None, **kwargs):
self._index = index.clone()
self._index._name = template
self._template_name = name
self.order = order

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

def to_dict(self):
d = self._index.to_dict()
d['index_patterns'] = [self._index._name]
if self.order is not None:
d['order'] = self.order
return d

def save(self, using=None):
Expand All @@ -51,11 +54,12 @@ def get_or_create_mapping(self):
self._mapping = Mapping()
return self._mapping

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

def resolve_nested(self, field_path):
for doc in self._doc_types:
Expand Down
9 changes: 9 additions & 0 deletions test_elasticsearch_dsl/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@ def test_conflicting_analyzer_raises_error():

with raises(ValueError):
i.analyzer('my_analyzer', tokenizer='keyword', filter=['lowercase', 'stop'])

def test_index_template_can_have_order():
i = Index('i-*')
it = i.as_template('i', order=2)

assert {
"index_patterns": ["i-*"],
"order": 2
} == it.to_dict()

0 comments on commit 85dafe6

Please sign in to comment.