Skip to content

Commit

Permalink
Enable setting up highlighting for multiple fields at once
Browse files Browse the repository at this point in the history
Thanks robhudson!
  • Loading branch information
honzakral committed Jan 23, 2015
1 parent fd8ff11 commit 59b2662
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 16 additions & 5 deletions elasticsearch_dsl/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,28 @@ def highlight_options(self, **kwargs):
s._highlight_opts.update(kwargs)
return s

def highlight(self, field, **kwargs):
def highlight(self, *fields, **kwargs):
"""
Request highliting of a field. All keyword arguments passed in will be
Request highliting of some fields. All keyword arguments passed in will be
used as parameters. Example::
Search().highlight('title', fragment_size=50)
Search().highlight('title', 'body', fragment_size=50)
will produce the equivalent of::
{
"highlight": {
"fields": {
"body": {"fragment_size": 50},
"title": {"fragment_size": 50}
}
}
}
:arg field: name of the field to be highlighted
"""
s = self._clone()
s._highlight[field] = kwargs
for f in fields:
s._highlight[f] = kwargs
return s

def index(self, *index):
Expand Down
5 changes: 3 additions & 2 deletions test_elasticsearch_dsl/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_complex_example():

s.query.minimum_should_match = 2

s = s.highlight_options(order='score').highlight('title', fragment_size=50)
s = s.highlight_options(order='score').highlight('title', 'body', fragment_size=50)

assert {
'query': {
Expand Down Expand Up @@ -199,7 +199,8 @@ def test_complex_example():
"highlight": {
'order': 'score',
'fields': {
'title': {'fragment_size': 50}
'title': {'fragment_size': 50},
'body': {'fragment_size': 50}
}
}
} == s.to_dict()
Expand Down

0 comments on commit 59b2662

Please sign in to comment.