+ form = None
+
+ # Remove any type prefix (pokemon:133) before constructing a query
+ if ':' in name:
+ prefix_chunk, name = name.split(':', 2)
+ prefixes = prefix_chunk.split(',')
+ if not valid_types:
+ # Only use types from the query string if none were explicitly
+ # provided
+ valid_types = prefixes
+
+ # 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) \
+ & whoosh.query.Term(u'forme_name', u'XXX')
+
+ # If there's a space in the input, this might be a form
+ if ' ' in name:
+ form, formless_name = name.split(' ', 2)
+ form_query = whoosh.query.Term(u'name', formless_name) \
+ & whoosh.query.Term(u'forme_name', form)
+ query = query | form_query
+
+ ### Filter by type of object
+ type_terms = []
+ for valid_type in valid_types:
+ if hasattr(valid_type, '__tablename__'):
+ table_name = getattr(valid_type, '__tablename__')
+ elif valid_type in indexed_tables:
+ table_name = valid_type
+ elif valid_type + 's' in indexed_tables:
+ table_name = valid_type + 's'
+ else:
+ # Bogus. Be nice and ignore it
+ continue
+
+ type_terms.append(whoosh.query.Term(u'table', table_name))
+
+ if type_terms:
+ query = query & whoosh.query.Or(type_terms)
+