Skip to content

Commit

Permalink
* Unifies IndexTemplate.save & Index.create methods to return raw res…
Browse files Browse the repository at this point in the history
…ults

* Removes default doctype constant
* PEP fixes
  • Loading branch information
0bsearch authored and honzakral committed Jul 27, 2019
1 parent 9da2dcf commit fa9f7d9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
12 changes: 6 additions & 6 deletions elasticsearch_dsl/index.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from . import analysis
from .connections import connections
from .search import Search
from .update_by_query import UpdateByQuery
from .exceptions import IllegalOperation
from .mapping import Mapping
from .search import Search
from .update_by_query import UpdateByQuery
from .utils import merge
from . import analysis

DEFAULT_DOC_TYPE = 'doc'

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

def save(self, using=None):
es = connections.get_connection(using or self._index._using)
es.indices.put_template(name=self._template_name, body=self.to_dict())
return es.indices.put_template(name=self._template_name, body=self.to_dict())


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

def is_closed(self, using=None):
state = self._get_connection(using).cluster.state(index=self._name, metric='metadata')
Expand Down
22 changes: 18 additions & 4 deletions test_elasticsearch_dsl/test_index.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from elasticsearch_dsl import Document, Index, Text, Date, analyzer, Mapping, \
exceptions

from random import choice
import string
from random import choice

from pytest import raises

from elasticsearch_dsl import Date, Document, Index, IndexTemplate, Text, analyzer


class Post(Document):
title = Text()
published_from = Date()


def test_multiple_doc_types_will_combine_mappings():
class User(Document):
username = Text()
Expand All @@ -27,12 +28,14 @@ class User(Document):
}
} == i.to_dict()


def test_search_is_limited_to_index_name():
i = Index('my-index')
s = i.search()

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


def test_cloned_index_has_copied_settings_and_using():
client = object()
i = Index('my-index', using=client)
Expand All @@ -45,6 +48,7 @@ def test_cloned_index_has_copied_settings_and_using():
assert i._settings == i2._settings
assert i._settings is not i2._settings


def test_cloned_index_has_analysis_attribute():
"""
Regression test for Issue #582 in which `Index.clone()` was not copying
Expand Down Expand Up @@ -75,6 +79,7 @@ def test_settings_are_saved():
}
} == i.to_dict()


def test_registered_doc_type_included_in_to_dict():
i = Index('i', using='alias')
i.document(Post)
Expand All @@ -88,6 +93,7 @@ def test_registered_doc_type_included_in_to_dict():
}
} == i.to_dict()


def test_registered_doc_type_included_in_search():
i = Index('i', using='alias')
i.document(Post)
Expand Down Expand Up @@ -135,13 +141,15 @@ def test_analyzers_returned_from_to_dict():

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


def test_conflicting_analyzer_raises_error():
i = Index('i')
i.analyzer('my_analyzer', tokenizer='whitespace', filter=['lowercase', 'stop'])

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)
Expand All @@ -150,3 +158,9 @@ def test_index_template_can_have_order():
"index_patterns": ["i-*"],
"order": 2
} == it.to_dict()


def test_index_template_save_result(mock_client):
it = IndexTemplate('test-template', 'test-*')

assert it.save(using='mock') == mock_client.indices.put_template()

0 comments on commit fa9f7d9

Please sign in to comment.