Skip to content

Commit 7d65231

Browse files
author
Mikolaj Rybinski
committed
QueryParser of super(WhooshSearchBackend, self).__init__(incl. FuzzyTermPlugin
1 parent f35e908 commit 7d65231

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

haystack/backends/whoosh_backend.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
EmptyResults,
2323
log_query,
2424
)
25-
from haystack.constants import DJANGO_CT, DJANGO_ID, ID
25+
from haystack.constants import (
26+
DJANGO_CT,
27+
DJANGO_ID,
28+
FUZZY_WHOOSH_MAX_EDITS,
29+
FUZZY_WHOOSH_MIN_PREFIX,
30+
ID,
31+
)
2632
from haystack.exceptions import MissingDependency, SearchBackendError, SkipDocument
2733
from haystack.inputs import Clean, Exact, PythonData, Raw
2834
from haystack.models import SearchResult
@@ -59,7 +65,7 @@
5965
from whoosh.filedb.filestore import FileStorage, RamStorage
6066
from whoosh.highlight import highlight as whoosh_highlight
6167
from whoosh.highlight import ContextFragmenter, HtmlFormatter
62-
from whoosh.qparser import QueryParser
68+
from whoosh.qparser import QueryParser, FuzzyTermPlugin
6369
from whoosh.searching import ResultsPage
6470
from whoosh.writing import AsyncWriter
6571

@@ -162,6 +168,7 @@ def setup(self):
162168
connections[self.connection_alias].get_unified_index().all_searchfields()
163169
)
164170
self.parser = QueryParser(self.content_field_name, schema=self.schema)
171+
self.parser.add_plugins([FuzzyTermPlugin])
165172

166173
if new_index is True:
167174
self.index = self.storage.create_index(self.schema)
@@ -959,7 +966,7 @@ def build_query_fragment(self, field, filter_type, value):
959966
"gte": "[%s to]",
960967
"lt": "{to %s}",
961968
"lte": "[to %s]",
962-
"fuzzy": "%s~",
969+
'fuzzy': "%s~{}/{}".format(FUZZY_WHOOSH_MAX_EDITS, FUZZY_WHOOSH_MIN_PREFIX),
963970
}
964971

965972
if value.post_process is False:

haystack/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
FUZZY_MIN_SIM = getattr(settings, "HAYSTACK_FUZZY_MIN_SIM", 0.5)
2121
FUZZY_MAX_EXPANSIONS = getattr(settings, "HAYSTACK_FUZZY_MAX_EXPANSIONS", 50)
2222

23+
# Default values on whoosh
24+
FUZZY_WHOOSH_MIN_PREFIX = getattr(settings, 'HAYSTACK_FUZZY_WHOOSH_MIN_PREFIX', 3)
25+
FUZZY_WHOOSH_MAX_EDITS = getattr(settings, 'HAYSTACK_FUZZY_WHOOSH_MAX_EDITS', 2)
26+
2327
# Valid expression extensions.
2428
VALID_FILTERS = set(
2529
[

0 commit comments

Comments
 (0)