- for suggestion in self.speller.suggest(
- name, self.INTERMEDIATE_LOOKUP_RESULTS):
-
- query = whoosh.query.Term('name', suggestion)
- results.extend(searcher.search(query))
+ fuzzy_query_parts = []
+ fuzzy_weights = {}
+ min_weight = [None]
+ for suggestion, _, weight in self.speller.suggestions_and_scores(name):
+ # Only allow the top 50% of scores; otherwise there will always
+ # be a lot of trailing junk
+ if min_weight[0] is None:
+ min_weight[0] = weight * 0.5
+ elif weight < min_weight[0]:
+ break
+
+ fuzzy_query_parts.append(whoosh.query.Term('name', suggestion))
+ fuzzy_weights[suggestion] = weight
+
+ if not fuzzy_query_parts:
+ # Nothing at all; don't try querying
+ return []
+
+ fuzzy_query = whoosh.query.Or(fuzzy_query_parts)
+ if type_term:
+ fuzzy_query = fuzzy_query & type_term
+
+ searcher.weighting = LanguageWeighting(extra_weights=fuzzy_weights)
+ results = searcher.search(fuzzy_query)