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.

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_document_id(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

Please note that although the source code for the bindings indicates that there's another constructor to xapian.Query that should be able to take a query operator, a list of Query objects, and an optional window, this doesn't actually work in Python at the moment due to the way SWIG overloading works. However, you can pass a query operator, a list of strings, and an optional window.

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 mymatcher(xapian.MatchDecider):
  def __init__(self):
    xapian.MatchDecider.__init__(self)

  def __call__(self, doc):
    return 1
Last updated $Date: 2004/12/08 16:14:33 $