Skip to content

Commit

Permalink
Add option to DocType.to_dict to produce the doc with all metadata
Browse files Browse the repository at this point in the history
Usable for example with elasticsearch.helpers.bulk (fixes #162)
  • Loading branch information
honzakral committed May 15, 2015
1 parent 7d53373 commit 21c30a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
25 changes: 23 additions & 2 deletions elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,43 @@ def delete(self, using=None, index=None, **kwargs):
if index is None:
raise #XXX - no index
# extract parent, routing etc from meta
doc_meta = dict((k, self.meta[k]) for k in DOC_META_FIELDS if k in self.meta)
doc_meta = dict(
(k, self.meta[k])
for k in META_FIELDS
if k in self.meta and k != 'index'
)
doc_meta.update(kwargs)
es.delete(
index=index,
doc_type=self._doc_type.name,
**doc_meta
)

def to_dict(self, include_meta=False):
d = super(DocType, self).to_dict()
if include_meta:
meta = dict(
('_' + k, self.meta[k])
for k in META_FIELDS
if k in self.meta
)
meta['_type'] = self._doc_type.name
meta['_source'] = d
d = meta
return d

def save(self, using=None, index=None, **kwargs):
es = self._get_connection(using)
if index is None:
index = getattr(self.meta, 'index', self._doc_type.index)
if index is None:
raise #XXX - no index
# extract parent, routing etc from meta
doc_meta = dict((k, self.meta[k]) for k in DOC_META_FIELDS if k in self.meta)
doc_meta = dict(
(k, self.meta[k])
for k in META_FIELDS
if k in self.meta and k != 'index'
)
doc_meta.update(kwargs)
meta = es.index(
index=index,
Expand Down
12 changes: 12 additions & 0 deletions test_elasticsearch_dsl/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class MyMultiSubDoc(MyDoc2, MySubDoc):
class DocWithNested(document.DocType):
comments = field.Nested(properties={'title': field.String()})

def test_to_dict_with_meta():
d = MyDoc(title='hello')
d.meta.parent = 'some-parent'
d.meta.index = 'other-index'

assert {
'_index': 'other-index',
'_parent': 'some-parent',
'_type': 'my_doc',
'_source': {'title': 'hello'},
} == d.to_dict(True)

def test_attribute_can_be_removed():
d = MyDoc(title='hello')

Expand Down

0 comments on commit 21c30a6

Please sign in to comment.