Skip to content

Fix suggest query names for Completion suggestor. #1098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions elasticsearch_dsl/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,21 +605,39 @@ def highlight(self, *fields, **kwargs):
s._highlight[f] = kwargs
return s

def suggest(self, name, text, **kwargs):
def suggest(self, name, text=None, regex=None, **kwargs):
"""
Add a suggestions request to the search.

:arg name: name of the suggestion
:arg text: text to suggest on
:arg regex: regex query for Completion Suggester

All keyword arguments will be added to the suggestions body. For example::

s = Search()
s = s.suggest('suggestion-1', 'Elasticsearch', term={'field': 'body'})

# regex query for Completion Suggester
s = Search()
s = s.suggest('suggestion-1', regex='py[thon|py]', completion={'field': 'body'})

"""
if text is None and regex is None:
raise ValueError('You have to pass "text" or "regex" argument.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need additional validation rules here - if you supply both text and regex or you supply regex and completion is not in kwargs etc - any invalida combination essentially should raise a ValueError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, i'll fix it more strictly.

if text and regex:
raise ValueError('You can only pass either "text" or "regex" argument.')
if regex and 'completion' not in kwargs:
raise ValueError('"regex" argument must be passed with "completion" keyword argument.')

s = self._clone()
s._suggest[name] = {'text': text}
s._suggest[name].update(kwargs)
d = s._suggest[name] = kwargs
if regex:
d['regex'] = regex
elif 'completion' in kwargs:
d['prefix'] = text
else:
d['text'] = text
return s

def to_dict(self, count=False, **kwargs):
Expand Down
47 changes: 47 additions & 0 deletions test_elasticsearch_dsl/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,53 @@ def test_suggest():
}
} == s.to_dict()


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

assert {
'suggest': {
'my_suggestion': {
'completion': {'field': 'title'},
'prefix': 'pyhton'
}
}
} == s.to_dict()


def test_suggest_regex_query():
s = search.Search()
s = s.suggest('my_suggestion', regex='py[thon|py]', completion={'field': 'title'})

assert {
'suggest': {
'my_suggestion': {
'completion': {'field': 'title'},
'regex': 'py[thon|py]'
}
}
} == s.to_dict()


def test_suggest_must_pass_text_or_regex():
s = search.Search()
with raises(ValueError):
s.suggest('my_suggestion')


def test_suggest_can_only_pass_text_or_regex():
s = search.Search()
with raises(ValueError):
s.suggest('my_suggestion', text='python', regex='py[hton|py]')


def test_suggest_regex_must_be_wtih_completion():
s = search.Search()
with raises(ValueError):
s.suggest('my_suggestion', regex='py[thon|py]')


def test_exclude():
s = search.Search()
s = s.exclude('match', title='python')
Expand Down