Skip to content

Commit

Permalink
Adding @rochabruno review.
Browse files Browse the repository at this point in the history
  • Loading branch information
ederfmartins committed Jan 22, 2016
1 parent 7292d99 commit 74ed990
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
32 changes: 27 additions & 5 deletions esengine/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def __init__(self, document_class=None, enable_all=True):
self.enable_all = enable_all

def _generate(self, doc_class):
"""
Generate the mapping acording to doc_class.
Args:
doc_class: esengine.Document object containing the model to be
mapped to elasticsearch.
"""
m = {
doc_class._doctype: {
"_all": {"enabled": self.enable_all},
Expand All @@ -39,6 +46,12 @@ def generate(self):
return self._generate(self.document_class)

def save(self, es=None):
"""
Save the mapping to index.
Args:
es: elasticsearch client intance.
"""
es = self.document_class.get_es(es)
if not es.indices.exists(index=self.document_class._index):
return es.indices.create(
Expand All @@ -52,23 +65,32 @@ def save(self, es=None):
body=self.generate()
)

def create_all(self, models_to_mapping, custon_settings, es=None):
def configure(self, models_to_mapping, custom_settings=None, es=None):
"""
Add custon settings like filters and analizers to index.
Add custon settings, like filters and analizers, to index. Be aware
that elasticsearch only allow this operation on index creation.
Args:
models_to_mapping: A list with the esengine.Document objects that
we want generate mapping.
custom_settings: a dict containing the configuration that will be
sent to elasticsearch/_settings (www.elastic.co/guide/en/
elasticsearch/reference/current/indices-update-settings.html)
es: elasticsearch client intance.
"""
if not isinstance(models_to_mapping, collections.Iterable):
raise AttributeError('models_to_mapping must be iterable')

mapped_models = [x for x in models_to_mapping]
if custon_settings:
if custom_settings:
indexes = set()
for model in mapped_models:
indexes.add(model._index)
if es is None:
es = model.get_es(es)
es = model.get_es(es)
for index in indexes:
if es.indices.exists(index=index):
msg = 'Settings are supported only on index creation'
Expand All @@ -79,7 +101,7 @@ def create_all(self, models_to_mapping, custon_settings, es=None):
mappings_by_index[model._index].update(mapping)
for index, mappings in mappings_by_index.items():
settings = {
"settings": custon_settings,
"settings": custom_settings,
"mappings": mappings
}
es.indices.create(index=index, body=settings)
Expand Down
25 changes: 17 additions & 8 deletions tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,34 @@ def test_change_format():
assert mapping['doc_type']['properties']['datefield']['format'] == pattern


def test_create_all_prerequiriments():
def test_configure_prerequiriments():
mapping = Mapping()
try:
mapping.create_all(10, None)
mapping.configure(10, None)
except AttributeError as e:
assert str(e) == 'models_to_mapping must be iterable'


def test_create_all_prerequiriments_throw_on_index_existence():
def test_configure_prerequiriments_throw_on_index_existence():
mapping = Mapping()
try:
models = [Doc, Doc1]
es = ESMock()
es.indices.exists_ret = True
mapping.create_all(models, True, es)
mapping.configure(models, True, es)
except ValueError as e:
assert str(e) == 'Settings are supported only on index creation'


def test_create_all_without_settings():
def test_configure_without_settings():
mapping = Mapping()
models = [Doc, Doc1]
mapping.create_all(models, None)
mapping.configure(models, None)
for model in models:
assert model.called


def test_create_all():
def test_configure():
mapping = Mapping()
models = [Doc, Doc1]
es = ESMock()
Expand All @@ -105,7 +105,7 @@ def test_create_all():
"my_analizer": "Another test"
}
}
mapping.create_all(models, settings, es)
mapping.configure(models, settings, es)
expected_mappings = {
'doc_type': {
'_all': {'enabled': True},
Expand Down Expand Up @@ -155,3 +155,12 @@ def create(self, index, body):
self.create_return[index] = body

indices = Indice()

def index(self, *args, **kwargs):
pass

def search(self, *args, **kwargs):
pass

def get(self, *args, **kwargs):
pass

0 comments on commit 74ed990

Please sign in to comment.