Skip to content

Commit 61846e0

Browse files
committed
fix: avoid an extra query on empty spelling suggestions
None was being used as a placeholder to test whether to run a spelling suggestion query but was also a possible response when the backend didn’t return a suggestion, which meant that calling `spelling_suggestion()` could run a duplicate query.
1 parent a45a618 commit 61846e0

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

haystack/backends/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
VALID_GAPS = ['year', 'month', 'day', 'hour', 'minute', 'second']
2020

21+
SPELLING_SUGGESTION_HAS_NOT_RUN = object()
2122

2223
def log_query(func):
2324
"""
@@ -469,7 +470,7 @@ def __init__(self, using=DEFAULT_ALIAS):
469470
self._hit_count = None
470471
self._facet_counts = None
471472
self._stats = None
472-
self._spelling_suggestion = None
473+
self._spelling_suggestion = SPELLING_SUGGESTION_HAS_NOT_RUN
473474
self.spelling_query = None
474475
self.result_class = SearchResult
475476
self.stats = {}
@@ -678,7 +679,7 @@ def get_spelling_suggestion(self, preferred_query=None):
678679
If the query has not been run, this will execute the query and store
679680
the results.
680681
"""
681-
if self._spelling_suggestion is None:
682+
if self._spelling_suggestion is SPELLING_SUGGESTION_HAS_NOT_RUN:
682683
self.run(spelling_query=preferred_query)
683684

684685
return self._spelling_suggestion
@@ -975,7 +976,7 @@ def _reset(self):
975976
self._results = None
976977
self._hit_count = None
977978
self._facet_counts = None
978-
self._spelling_suggestion = None
979+
self._spelling_suggestion = SPELLING_SUGGESTION_HAS_NOT_RUN
979980

980981
def _clone(self, klass=None, using=None):
981982
if using is None:

0 commit comments

Comments
 (0)