apt-xapian-index: adding simple result filters to the query

I've recently posted an introduction of apt-xapian-index and an example of how to query it.

Today I'll show how to add simple Debtags-based result filters to a query. I'll expand axi-query-simple.py with a --type comman line option that allows to filter for package type. As a starter I'll implement filtering for "game", "gui", "cmdline" or "editor".

If you feel like reimplementing my examples in another language, let me know and I'll include it to the post.

The filters are implemented using Xapian queries. Xapian allows to build queries as well as combine existing queries using a large amount of operators.

This allows us to create a little database of ready made queries:

# This is our little database of simple Debtags filters we provide: the name
# entered by the user in "--type" maps to a piece of Xapian query
filterdb = dict(
    # We can do simple AND queries...
    game = xapian.Query(xapian.Query.OP_AND, ('XTuse::gameplaying', 'XTrole::program')),
    # Or we can do complicate binary expressions...
    gui = xapian.Query(xapian.Query.OP_AND, xapian.Query('XTrole::program'),
        xapian.Query(xapian.Query.OP_OR, 'XTinterface::x11', 'XTinterface::3d')),
    cmdline = xapian.Query(xapian.Query.OP_AND, 'XTrole::program', 'XTinterface::commandline'),
    editor = xapian.Query(xapian.Query.OP_AND, 'XTrole::program', 'XTuse::editing'))
    # Feel free to invent more

It's now trivial to lookup a query and AND it to the main query:

# Build the query by ORing together various terms
query = xapian.Query(xapian.Query.OP_OR, termlist)

# If a filter was requested, AND it with the query
if extrafilter:
    query = xapian.Query(xapian.Query.OP_AND, query, filterdb[extrafilter])

That's it. Simple, trivial feature to add to a search, but very useful and simple to use.

You can use the wsvn interface to get to the full source code and the module it uses. Try running it as:

axi-query-pkgtype.py --type=gui image editor
axi-query-pkgtype.py --type=cmdline image editor
axi-query-pkgtype.py --type=game image editor

you will see that the results change radically.

What are the games that match "image editor", you would ask?

74% xjig - An X11 jigsaw puzzle
69% wmpuzzle - WindowMaker dock app 4x4 puzzle
66% xflip - programs to mirror-image or melt your display

Jigsaws, of course :)

The code is now colourised thanks to Simon Ward who pointed me at the syntax plugin for ikiwiki.

Next in the series: suggesting new terms for improving the query.