Skip to content

Commit

Permalink
Implementing the suggest functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
honzakral committed Jan 29, 2015
1 parent cbd1376 commit 34c5545
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/search_dsl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ Enabling highlighting for individual fields is done using the ``highlight`` meth
# or, including parameters:
s = s.highlight('title', fragment_size=50)
Suggestions
~~~~~~~~~~~

To specify a suggest request on your ``Search`` object use the ``suggest`` method:

.. code:: python
s = s.suggest('my_suggestion', 'pyhton', term={'field': 'title'})
The first argument is the name of the suggestions (name under which it will be
returned), second is the actual text you wish the suggester to work on and the
keyword arguments will be added to the suggest's json as-is.

Extra properties and parameters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
17 changes: 17 additions & 0 deletions elasticsearch_dsl/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def __init__(self, using='default', index=None, doc_type=None, extra=None):
self._fields = None
self._highlight = {}
self._highlight_opts = {}
self._suggest = {}

def __getitem__(self, n):
"""
Expand Down Expand Up @@ -155,6 +156,7 @@ def _clone(self):
s._extra = self._extra.copy()
s._highlight = self._highlight.copy()
s._highlight_opts = self._highlight_opts.copy()
s._suggest = self._suggest.copy()
for x in ('query', 'filter', 'post_filter'):
getattr(s, x)._proxied = getattr(self, x)._proxied

Expand Down Expand Up @@ -192,6 +194,12 @@ def update_from_dict(self, d):
high = d.pop('highlight').copy()
self._highlight = high.pop('fields')
self._highlight_opts = high
if 'suggest' in d:
self._suggest = d.pop('suggest')
if 'text' in self._suggest:
text = self._suggest.pop('text')
for s in self._suggest.values():
s.setdefault('text', text)
self._extra = d

def params(self, **kwargs):
Expand Down Expand Up @@ -287,6 +295,12 @@ def highlight(self, *fields, **kwargs):
s._highlight[f] = kwargs
return s

def suggest(self, name, text, **kwargs):
s = self._clone()
s._suggest[name] = {'text': text}
s._suggest[name].update(kwargs)
return s

def index(self, *index):
"""
Set the index for the search. If called empty it will rmove all information.
Expand Down Expand Up @@ -378,6 +392,9 @@ def to_dict(self, count=False, **kwargs):
d['highlight'] = {'fields': self._highlight}
d['highlight'].update(self._highlight_opts)

if self._suggest:
d['suggest'] = self._suggest

d.update(kwargs)
return d

Expand Down
51 changes: 51 additions & 0 deletions test_elasticsearch_dsl/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ def test_reverse():
'fields': {
'title': {'fragment_size': 50}
}
},
"suggest": {
"my-title-suggestions-1" : {
"text" : "devloping distibutd saerch engies",
"term" : {
"size" : 3,
"field" : "title"
}
}
}
}

Expand Down Expand Up @@ -329,3 +338,45 @@ def test_fields_on_clone():
},
'fields': ['title']
} == search.Search().fields(['title']).filter('term', title='python').to_dict()

def test_suggest_accepts_global_text():
s = search.Search.from_dict({
"query": {"match_all": {}},
"suggest" : {
"text" : "the amsterdma meetpu",
"my-suggest-1" : {
"term" : {"field" : "title"}
},
"my-suggest-2" : {
"text": "other",
"term" : {"field" : "body"}
}
}
})

assert {
'query': {'match_all': {}},
'suggest': {
'my-suggest-1': {
'term': {'field': 'title'},
'text': 'the amsterdma meetpu'
},
'my-suggest-2': {
'term': {'field': 'body'},
'text': 'other'}
}
} == s.to_dict()

def test_suggest():
s = search.Search()
s = s.suggest('my_suggestion', 'pyhton', term={'field': 'title'})

assert {
'query': {'match_all': {}},
'suggest': {
'my_suggestion': {
'term': {'field': 'title'},
'text': 'pyhton'
}
}
} == s.to_dict()

0 comments on commit 34c5545

Please sign in to comment.