Skip to content

Commit

Permalink
Added a highlight dictionary to DocType.meta (closes #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcwatson committed Mar 4, 2015
1 parent 3e3d143 commit 538a32a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def get(cls, id, using=None, index=None, **kwargs):
def from_es(cls, hit):
# don't modify in place
meta = hit.copy()
# make sure highlighting information ends up in meta
meta['_highlight'] = meta.pop('highlight', {})
doc = meta.pop('_source')
return cls(meta=meta, **doc)

Expand Down
13 changes: 11 additions & 2 deletions test_elasticsearch_dsl/test_integration/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Meta:
class Commit(DocType):
committed_date = Date()
authored_date = Date()
description = String(analyzer='snowball')

class Meta:
index = 'git'
Expand Down Expand Up @@ -76,9 +77,9 @@ def test_delete(write_client):
)

test_repo = Repository(meta={'id': 'elasticsearch-dsl-py'})
test_repo.meta.index='test-document'
test_repo.meta.index = 'test-document'
test_repo.delete()

assert not write_client.exists(
index='test-document',
doc_type='repos',
Expand Down Expand Up @@ -109,3 +110,11 @@ class Meta:
assert 'description' in Commit._doc_type.mapping
assert 'committed_date' in Commit._doc_type.mapping
assert isinstance(Commit._doc_type.mapping['committed_date'], Date)

def test_highlight_in_meta(data_client):
commit = Commit.search().query('match', description='inverting').highlight('description').execute()[0]

assert isinstance(commit, Commit)
assert 'description' in commit.meta.highlight
assert isinstance(commit.meta.highlight['description'], list)
assert len(commit.meta.highlight['description']) > 0

0 comments on commit 538a32a

Please sign in to comment.