+ form = None
+
+ # Remove any type prefix (pokemon:133) before constructing a query
+ if ':' in name:
+ prefix_chunk, name = name.split(':', 1)
+ name = name.strip()
+
+ if not valid_types:
+ # Only use types from the query string if none were explicitly
+ # provided
+ prefixes = prefix_chunk.split(',')
+ valid_types = [_.strip() for _ in prefixes]
+
+ # Random lookup
+ if name == 'random':
+ return random_lookup(indices=(index, speller),
+ session=session,
+ valid_types=valid_types)
+
+ # Do different things depending what the query looks like
+ # Note: 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 '*' in name or '?' in name:
+ exact_only = True
+ query = whoosh.query.Wildcard(u'name', name)
+ elif 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(' ', 1)
+ 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:
+ table_name = _parse_table_name(valid_type)
+ type_terms.append(whoosh.query.Term(u'table', table_name))
+
+ if type_terms:
+ query = query & whoosh.query.Or(type_terms)