+ return objects[:MAX_LOOKUP_RESULTS]
+
+
+def random_lookup(valid_types=[], session=None, indices=None):
+ """Takes similar arguments as `lookup()`, but returns a random lookup
+ result from one of the provided `valid_types`.
+ """
+
+ tables = []
+ for valid_type in valid_types:
+ table_name = _parse_table_name(valid_type)
+ if table_name:
+ tables.append(indexed_tables[table_name])
+
+ if not tables:
+ # n.b.: It's possible we got a list of valid_types and none of them
+ # were valid, but this function is guaranteed to return *something*, so
+ # it politely selects from the entire index isntead
+ tables = indexed_tables.values()
+
+ # Rather than create an array of many hundred items and pick randomly from
+ # it, just pick a number up to the total number of potential items, then
+ # pick randomly from that, and partition the whole range into chunks.
+ # This also avoids the slight problem that the index contains more rows
+ # (for languages) for some items than others.
+ # XXX ought to cache this (in the index?) if possible
+ total = 0
+ partitions = []
+ for table in tables:
+ count = session.query(table).count()
+ total += count
+ partitions.append((table, count))
+
+ n = random.randint(1, total)
+ while n > partitions[0][1]:
+ n -= partitions[0][1]
+ partitions.pop(0)
+
+ return lookup(unicode(n), valid_types=[ partitions[0][0] ],
+ indices=indices, session=session)
+
+def prefix_lookup(prefix, session=None, indices=None):
+ """Returns terms starting with the given exact prefix.
+
+ No special magic is currently done with the name; type prefixes are not
+ recognized.
+
+ `session` and `indices` are treated as with `lookup()`.
+ """
+
+ if not session:
+ session = connect()
+
+ if indices:
+ index, speller = indices
+ else:
+ index, speller = open_index()
+
+ query = whoosh.query.Prefix(u'name', normalize(prefix))
+
+ searcher = index.searcher()
+ searcher.weighting = LanguageWeighting()
+ results = searcher.search(query) # XXX , limit=MAX_LOOKUP_RESULTS)
+
+ return _whoosh_records_to_results(results, session)