+ 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:
+ 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
+ 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', prefix.lower())
+
+ searcher = index.searcher()
+ searcher.weighting = LanguageWeighting()
+ results = searcher.search(query) # XXX , limit=MAX_LOOKUP_RESULTS)
+
+ return _whoosh_records_to_results(results, session)