Skip to content

Commit

Permalink
added six
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Jan 7, 2016
1 parent ba2b749 commit 4cab9a9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
71 changes: 62 additions & 9 deletions esengine/document.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import elasticsearch.helpers as eh

from six import iteritems
from esengine.bases.document import BaseDocument
from esengine.bases.metaclass import ModelMetaclass
from esengine.bases.result import ResultSet
Expand Down Expand Up @@ -85,28 +86,80 @@ def save(self, es=None):
id=self.id, # noqa
body=doc
)
if saved_document.get('created'):
created = saved_document.get('created')
if created:
self.id = saved_document['_id']
return created

def update(self, es=None, meta=None, **kwargs):
def update(self, body=None, es=None, meta=None, **kwargs):
"""
Update a single document
Using fields
>>> Document().update(some_field="some_value")
Using a body dict
>>> Document().update({'some_field': "some_value"})
Or a script
>>> Document().update(script="for(x in data){x}",
... lang="groovy",
... params={'data': [...]})
:param es: ES client
:param meta: Extra values to be passed to client
:param body: Optional values passed as dict
:param kwargs: values to change
:return: Update result
"""
body = body or {}
body.update(kwargs)
updated_data = self.update_by_id(
self.id, body=body, es=es, meta=meta, **kwargs
)
if 'script' not in kwargs:
for key, value in iteritems(body):
setattr(self, key, value)
return updated_data

@classmethod
def update_by_id(cls, doc_id, body=None, es=None, meta=None, **kwargs):
"""
Update a single document using its id on BaseClass
Using fields
>>> Document.update_by_id(1234, some_field="some_value")
Using boy dict
>>> Document.update_by_id(1234, {'some_field': 'some_value'})
Or a script
>>> Document.update_by_id(1234,
... script="for(x in data){x}",
... lang="groovy",
... params={'data': [...]})
:param doc_id: The document of the id to be updated
:param body: Optional values passed as dict
:param es: ES client
:param meta: Extra values to be passed to client
:param kwargs: values to change
:return: Update result
"""
body = body or {}
body.update(kwargs)
meta = meta or {}
if 'retry_on_conflict' not in meta:
meta = {'retry_on_conflict': 5}
return self.get_es(es).update(
index=self._index,
doc_type=self._doctype,
id=self.id, # noqa
body=kwargs,

if 'script' not in body and 'doc' not in body:
body = {'doc': body}

updated_data = cls.get_es(es).update(
index=cls._index,
doc_type=cls._doctype,
id=doc_id, # noqa
body=body,
**meta
)
return updated_data

def delete(self, es=None):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"es1": ["elasticsearch>=1.0.0,<2.0.0"],
"es2": ["elasticsearch>=2.0.0,<3.0.0"]
},
install_requires=["python-dateutil"],
install_requires=["python-dateutil", "six==1.10.0"],
tests_require=[
"pytest==2.8.3",
"pytest-cov==2.2.0",
Expand Down
1 change: 1 addition & 0 deletions test.req
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# base
elasticsearch>=1.0.0,<2.0.0
python-dateutil
six==1.10.0

# testing
coveralls
Expand Down

0 comments on commit 4cab9a9

Please sign in to comment.