- # 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
+ # If the input provided is a number, match it as an id. Otherwise, name.
+ # Term objects do an exact match, so we don't have to worry about a query
+ # parser tripping on weird characters in the input
+ if rx_is_number.match(name):
+ # Don't spell-check numbers!
+ exact_only = True
+ query = whoosh.query.Term(u'row_id', name)
+ else:
+ # Not an integer
+ query = whoosh.query.Term(u'name', name)
+
+ ### Actual searching
+ searcher = index.searcher()
+ searcher.weighting = LanguageWeighting() # XXX kosher? docs say search()
+ # takes a weighting kw but it
+ # certainly does not
+ 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, 25):
+ query = whoosh.query.Term('name', suggestion)
+ results.extend(searcher.search(query))
+
+ ### Convert results to db objects