apt-xapian-index: search as you type
I've recently posted:
Note that I've rewritten all the old posts to only show the main code snippets: if you were put off by the large lumps of code, you may want to
give it another go.
Today I'll show how to implement a very attractive feature for a user
interface: search as you type. The idea is that you don't need to press enter
to fire up a query: instead, the results materialise in front of your eyes as
you type them.
The example I created uses curses, but the idea is good on any interactive user
interface.
The main thing to keep in mind with search as you type is that the last word is
likely to be partially typed, unless maybe some timeout expired since the
user's last keystroke.
Xapian comes into help here, as it allows us to expand the partially typed word
into an OR query with all the terms that start with it. This means that if we
are typing, for example, "progr", we can turn the query into "program OR
programmer OR programming OR programmed [...and so on...]".
I won't show the UI code, except a simple input loop that triggers the query at
every keystroke:
def mainloop(self):
while True:
c = self.win.getch()
self.line += chr(c)
self.results.update(self.line)
The interesting part is in the
update function.
First we split the line in words and convert the words into a query:
args = self.splitline.split(line)
terms = termsForSimpleQuery(args)
Then we expand the last word with all possible completions:
terms.extend([x.term for x in db.allterms(args[-1])])
Now we can build the query. Of course you can add all other sorts of things to
the query, for example a boolean expression of tag filter like in
axi-query-pkgtype.py;
Xapian will cope.
query = xapian.Query(xapian.Query.OP_OR, terms)
Finally the query. For bonus points you can do the
adaptive
cutoff
trick to discard bad results.
In my case, since I don't implement scrolling of results, I also limit them to
what fits in the window:
mset = enquire.get_mset(0, self.size - 1)
Finally, draw the results on screen:
self.win.clear()
self.win.addstr(0, 0, "%i results found." % mset.get_matches_estimated(), curses.A_BOLD)
for y, m in enumerate(mset):
name = m[xapian.MSET_DOCUMENT].get_data()
pkg = cache[name]
self.win.addstr(y+1, 0, "%i%% %s - %s" % (m[xapian.MSET_PERCENT], name, pkg.summary))
self.win.refresh()
That's it, try it out.
You can use the
wsvn interface to get to the
full source code and the
module it uses.
You can see a similar technique working in
goplay, where it is also integrated
with an interactive tag filter.