- # Alas! We have to make three attempts to find anything with this index.
- # First: Try an exact match for a name in the index.
- # Second: Try an exact match for a stripped-down name in the index.
- # Third: Get spelling suggestions.
- # The spelling module apparently only indexes *words* -- that is, [a-z]+.
- # So we have a separate field that contains the same name, stripped down to
- # just [a-z]+.
- # Unfortunately, exact matches aren't returned as spelling suggestions, so
- # we also have to do a regular index match against this separate field.
- # Otherwise, 'nidoran' will never match anything
- index, speller = get_index(session)
-
- # Look for exact name
- parser = QueryParser('name', schema=index.schema)
- results = index.find(name.lower(), parser=parser)
-
- if not exact_only:
- # Look for a match with a reduced a-z name
- if not results:
- parser = QueryParser('spelling_name', schema=index.schema)
- results = index.find(name.lower(), parser=parser)
-
- # Look for some fuzzy matches
- if not results:
- results = []
- exact = False
-
- for suggestion in speller.suggest(name, 3):
- results.extend( index.find(suggestion, parser=parser) )
-
- # Convert results to db objects
+ # Look for exact name. A Term object does an exact match, so we don't have
+ # to worry about a query parser tripping on weird characters in the input
+ searcher = index.searcher()
+ query = whoosh.query.Term('name', name.lower())
+ results = searcher.search(query)
+
+ # Look for some fuzzy matches if necessary
+ if not exact_only and not results:
+ exact = False
+ results = []
+
+ for suggestion in speller.suggest(name, 10):
+ query = whoosh.query.Term('name', suggestion)
+ results.extend(searcher.search(query))
+
+ ### Convert results to db objects