Skip to content

Commit fa9f7d9

Browse files
0bsearchhonzakral
authored andcommitted
* Unifies IndexTemplate.save & Index.create methods to return raw results
* Removes default doctype constant * PEP fixes
1 parent 9da2dcf commit fa9f7d9

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

elasticsearch_dsl/index.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
from . import analysis
12
from .connections import connections
2-
from .search import Search
3-
from .update_by_query import UpdateByQuery
43
from .exceptions import IllegalOperation
54
from .mapping import Mapping
5+
from .search import Search
6+
from .update_by_query import UpdateByQuery
67
from .utils import merge
7-
from . import analysis
88

9-
DEFAULT_DOC_TYPE = 'doc'
109

1110
class IndexTemplate(object):
1211
def __init__(self, name, template, index=None, order=None, **kwargs):
@@ -33,7 +32,8 @@ def to_dict(self):
3332

3433
def save(self, using=None):
3534
es = connections.get_connection(using or self._index._using)
36-
es.indices.put_template(name=self._template_name, body=self.to_dict())
35+
return es.indices.put_template(name=self._template_name, body=self.to_dict())
36+
3737

3838
class Index(object):
3939
def __init__(self, name, using='default'):
@@ -255,7 +255,7 @@ def create(self, using=None, **kwargs):
255255
Any additional keyword arguments will be passed to
256256
``Elasticsearch.indices.create`` unchanged.
257257
"""
258-
self._get_connection(using).indices.create(index=self._name, body=self.to_dict(), **kwargs)
258+
return self._get_connection(using).indices.create(index=self._name, body=self.to_dict(), **kwargs)
259259

260260
def is_closed(self, using=None):
261261
state = self._get_connection(using).cluster.state(index=self._name, metric='metadata')

test_elasticsearch_dsl/test_index.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from elasticsearch_dsl import Document, Index, Text, Date, analyzer, Mapping, \
2-
exceptions
3-
4-
from random import choice
51
import string
2+
from random import choice
63

74
from pytest import raises
85

6+
from elasticsearch_dsl import Date, Document, Index, IndexTemplate, Text, analyzer
7+
8+
99
class Post(Document):
1010
title = Text()
1111
published_from = Date()
1212

13+
1314
def test_multiple_doc_types_will_combine_mappings():
1415
class User(Document):
1516
username = Text()
@@ -27,12 +28,14 @@ class User(Document):
2728
}
2829
} == i.to_dict()
2930

31+
3032
def test_search_is_limited_to_index_name():
3133
i = Index('my-index')
3234
s = i.search()
3335

3436
assert s._index == ['my-index']
3537

38+
3639
def test_cloned_index_has_copied_settings_and_using():
3740
client = object()
3841
i = Index('my-index', using=client)
@@ -45,6 +48,7 @@ def test_cloned_index_has_copied_settings_and_using():
4548
assert i._settings == i2._settings
4649
assert i._settings is not i2._settings
4750

51+
4852
def test_cloned_index_has_analysis_attribute():
4953
"""
5054
Regression test for Issue #582 in which `Index.clone()` was not copying
@@ -75,6 +79,7 @@ def test_settings_are_saved():
7579
}
7680
} == i.to_dict()
7781

82+
7883
def test_registered_doc_type_included_in_to_dict():
7984
i = Index('i', using='alias')
8085
i.document(Post)
@@ -88,6 +93,7 @@ def test_registered_doc_type_included_in_to_dict():
8893
}
8994
} == i.to_dict()
9095

96+
9197
def test_registered_doc_type_included_in_search():
9298
i = Index('i', using='alias')
9399
i.document(Post)
@@ -135,13 +141,15 @@ def test_analyzers_returned_from_to_dict():
135141

136142
assert index.to_dict()["settings"]["analysis"]["analyzer"][random_analyzer_name] == {"filter": ["standard"], "type": "custom", "tokenizer": "standard"}
137143

144+
138145
def test_conflicting_analyzer_raises_error():
139146
i = Index('i')
140147
i.analyzer('my_analyzer', tokenizer='whitespace', filter=['lowercase', 'stop'])
141148

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

152+
145153
def test_index_template_can_have_order():
146154
i = Index('i-*')
147155
it = i.as_template('i', order=2)
@@ -150,3 +158,9 @@ def test_index_template_can_have_order():
150158
"index_patterns": ["i-*"],
151159
"order": 2
152160
} == it.to_dict()
161+
162+
163+
def test_index_template_save_result(mock_client):
164+
it = IndexTemplate('test-template', 'test-*')
165+
166+
assert it.save(using='mock') == mock_client.indices.put_template()

0 commit comments

Comments
 (0)