Python bindings for Xapian

The Python bindings for Xapian are packaged in the xapian module, and largely follow the C++ API, with the following differences and additions. Python strings and lists, etc., are converted automatically in the bindings, so generally it should just work as expected.

The examples subdirectory contains examples showing how to use the Python bindings based on the simple examples from xapian-examples: simpleindex.py, simplesearch.py, simpleexpand.py. There's also simplematchdecider.py which shows how to define a MatchDecider in Python.

Exceptions

Exceptions are thrown as SWIG exceptions instead of Xapian exceptions. This isn't done well at the moment; in future we will throw wrapped Xapian exceptions. For now, it's probably easier to catch all exceptions and try to take appropriate action based on their associated string.

Iterators

All iterators support next() and equals() methods to move through and test iterators (as for all language bindings). MSetIterator and ESetIterator also support prev(). Python-wrapped iterators also support direct comparison, so something like:

   m=mset.begin()
   while m!=mset.end():
     # do something
     m.next()

Iterator dereferencing

C++ iterators are often dereferenced to get information, eg (*it). With Python these are all mapped to named methods, as follows:

IteratorDereferencing method
PositionIterator get_termpos()
PostingIterator get_docid()
TermIterator get_term()
ValueIterator get_value()
MSetIterator get_docid()
ESetIterator get_termname()

Other methods, such as MSetIterator.get_document(), are available unchanged.

Pythonic iterators

Provided you're using Python 2.2 or newer, many classes that support C++-style iterators also support Pythonic iterators which do the same thing in a Python style. The following are supported (where marked as default iterator, it means __iter__() does the right thing so you can for instance use for term in document to iterate over terms in the Document):

ClassMethodEquivalent toIterator type
MSetdefault iteratorbegin()MSetIter
ESetdefault iteratorbegin()ESetIter
Enquirematching_terms()get_matching_terms_begin()TermIter
Querydefault iteratorget_terms_begin()TermIter
Databaseallterms()allterms_begin() (also as default iterator)TermIter
Databasepostlist(tname)postlist_begin(tname)PostingIter
Databasetermlist(docid)termlist_begin(docid)TermIter
Databasepositionlist(docid, tname)positionlist_begin(docid, tname)PositionIter
Documentvalues()values_begin()ValueIter
Documenttermlist()termlist_begin() (also as default iterator)TermIter
QueryParserstoplist()stoplist_begin()TermIter
QueryParserunstemlist(tname)unstem_begin(tname)TermIter

The Pythonic iterators will all return lists representing the appropriate item when their next() method is called, except PositionIter which just returns a single value:

ClassReturns
MSetIter[docid, weight, rank, percentage, document]
ESetIter[termname, weight]
TermIter[term, wdf, termfreq, position iterator]
PostingIter[docid, doclength, wdf, position iterator]
PositionItertermpos
ValueIter[valueno, value]

MSet

MSet objects have some additional methods to simplify access (these work using the C++ array dereferencing):

Method nameExplanation
get_hit(index)returns MSetIterator at index
get_document_percentage(index)convert_to_percent(get_hit(index))
get_document(index)get_hit(index).get_document()
get_docid(index)get_hit(index).get_docid()

Additionally, the MSet has a property, mset.items, which returns a list of tuples representing the MSet; this may be more convenient than using the MSetIterator. The members of the tuple are as follows.

IndexContents
xapian.MSET_DIDDocument id
xapian.MSET_WTWeight
xapian.MSET_RANKRank
xapian.MSET_PERCENTPercentage weight

Two MSet objects are equal if they have the same number and maximum possible number of members, and if every document member of the first MSet exists at the same index in the second MSet, with the same weight.

ESet

The ESet has a property, eset.items, which returns a list of tuples representing the ESet; this may be more convenient than using the ESetIterator. The members of the tuple are as follows.

IndexContents
xapian.ESET_TNAMETerm name
xapian.ESET_WTWeight

Database Factory Functions

Query

In C++ there's a Xapian::Query constructor which takes a query operator and start/end iterators specifying a number of terms or queries, plus an optional parameter. In Python, this is wrapped to accept any Python sequence (for example a list or tuple) to give the terms/queries, and you can specify a mixture of terms and queries if you wish. For example:

   subq = xapian.Query(xapian.Query.OP_AND, "hello", "world")
   q = xapian.Query(xapian.Query.OP_AND, [subq, "foo", xapian.Query("bar", 2)])

Enquire

There is an additional method get_matching_terms() which takes an MSetIterator and returns a list of terms in the current query which match the document given by that iterator. You may find this more convenient than using the TermIterator directly.

MatchDecider

Custom MatchDeciders can be created in Python; simply subclass xapian.MatchDecider, ensure you call the super-constructor, and define a __call__ method that will do the work. The simplest example (which does nothing useful) would be as follows:

class mymatchdecider(xapian.MatchDecider):
  def __init__(self):
    xapian.MatchDecider.__init__(self)

  def __call__(self, doc):
    return 1
Last updated $Date: 2006-10-10 16:04:08 +0100 (Tue, 10 Oct 2006) $