Skip to content

Commit 25dfc31

Browse files
Caiofcastell-kmiguelgrinberg
authored
Fix suggest query names for Completion suggestor. (Finish work on #1098) (#1836)
* Add search_analyzer. * Fix suggest query names for Completion suggestor. * Fix that arguments validation is more strictly. * refactor: add type hints to wrappers.py * Revert "refactor: add type hints to wrappers.py" This reverts commit 90f43ca. * feat: readd regex implementation --------- Co-authored-by: tell-k <[email protected]> Co-authored-by: Miguel Grinberg <[email protected]>
1 parent c9612c1 commit 25dfc31

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

Diff for: elasticsearch_dsl/search_base.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ def highlight(self, *fields, **kwargs):
743743
s._highlight[f] = kwargs
744744
return s
745745

746-
def suggest(self, name, text, **kwargs):
746+
def suggest(self, name, text=None, regex=None, **kwargs):
747747
"""
748748
Add a suggestions request to the search.
749749
@@ -754,9 +754,27 @@ def suggest(self, name, text, **kwargs):
754754
755755
s = Search()
756756
s = s.suggest('suggestion-1', 'Elasticsearch', term={'field': 'body'})
757+
758+
# regex query for Completion Suggester
759+
s = Search()
760+
s = s.suggest('suggestion-1', regex='py[thon|py]', completion={'field': 'body'})
757761
"""
762+
if text is None and regex is None:
763+
raise ValueError('You have to pass "text" or "regex" argument.')
764+
if text and regex:
765+
raise ValueError('You can only pass either "text" or "regex" argument.')
766+
if regex and "completion" not in kwargs:
767+
raise ValueError(
768+
'"regex" argument must be passed with "completion" keyword argument.'
769+
)
770+
758771
s = self._clone()
759-
s._suggest[name] = {"text": text}
772+
if regex:
773+
s._suggest[name] = {"regex": regex}
774+
elif "completion" in kwargs:
775+
s._suggest[name] = {"prefix": text}
776+
else:
777+
s._suggest[name] = {"text": text}
760778
s._suggest[name].update(kwargs)
761779
return s
762780

Diff for: tests/_async/test_search.py

+40
Original file line numberDiff line numberDiff line change
@@ -718,3 +718,43 @@ async def test_empty_search():
718718
assert [hit async for hit in s] == []
719719
assert [hit async for hit in s.scan()] == []
720720
await s.delete() # should not error
721+
722+
723+
def test_suggest_completion():
724+
s = AsyncSearch()
725+
s = s.suggest("my_suggestion", "pyhton", completion={"field": "title"})
726+
727+
assert {
728+
"suggest": {
729+
"my_suggestion": {"completion": {"field": "title"}, "prefix": "pyhton"}
730+
}
731+
} == s.to_dict()
732+
733+
734+
def test_suggest_regex_query():
735+
s = AsyncSearch()
736+
s = s.suggest("my_suggestion", regex="py[thon|py]", completion={"field": "title"})
737+
738+
assert {
739+
"suggest": {
740+
"my_suggestion": {"completion": {"field": "title"}, "regex": "py[thon|py]"}
741+
}
742+
} == s.to_dict()
743+
744+
745+
def test_suggest_must_pass_text_or_regex():
746+
s = AsyncSearch()
747+
with raises(ValueError):
748+
s.suggest("my_suggestion")
749+
750+
751+
def test_suggest_can_only_pass_text_or_regex():
752+
s = AsyncSearch()
753+
with raises(ValueError):
754+
s.suggest("my_suggestion", text="python", regex="py[hton|py]")
755+
756+
757+
def test_suggest_regex_must_be_wtih_completion():
758+
s = AsyncSearch()
759+
with raises(ValueError):
760+
s.suggest("my_suggestion", regex="py[thon|py]")

Diff for: tests/_sync/test_search.py

+40
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,43 @@ def test_empty_search():
716716
assert [hit for hit in s] == []
717717
assert [hit for hit in s.scan()] == []
718718
s.delete() # should not error
719+
720+
721+
def test_suggest_completion():
722+
s = Search()
723+
s = s.suggest("my_suggestion", "pyhton", completion={"field": "title"})
724+
725+
assert {
726+
"suggest": {
727+
"my_suggestion": {"completion": {"field": "title"}, "prefix": "pyhton"}
728+
}
729+
} == s.to_dict()
730+
731+
732+
def test_suggest_regex_query():
733+
s = Search()
734+
s = s.suggest("my_suggestion", regex="py[thon|py]", completion={"field": "title"})
735+
736+
assert {
737+
"suggest": {
738+
"my_suggestion": {"completion": {"field": "title"}, "regex": "py[thon|py]"}
739+
}
740+
} == s.to_dict()
741+
742+
743+
def test_suggest_must_pass_text_or_regex():
744+
s = Search()
745+
with raises(ValueError):
746+
s.suggest("my_suggestion")
747+
748+
749+
def test_suggest_can_only_pass_text_or_regex():
750+
s = Search()
751+
with raises(ValueError):
752+
s.suggest("my_suggestion", text="python", regex="py[hton|py]")
753+
754+
755+
def test_suggest_regex_must_be_wtih_completion():
756+
s = Search()
757+
with raises(ValueError):
758+
s.suggest("my_suggestion", regex="py[thon|py]")

0 commit comments

Comments
 (0)