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 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.
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()
C++ iterators are often dereferenced to get information, eg
(*it)
. With Python these are all mapped to named methods, as
follows:
Iterator | Dereferencing 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.
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):
Class | Method | Equivalent to | Iterator type |
MSet | default iterator | begin() | MSetIter |
ESet | default iterator | begin() | ESetIter |
Enquire | matching_terms() | get_matching_terms_begin() | TermIter |
Query | default iterator | get_terms_begin() | TermIter |
Database | allterms() | allterms_begin() (also as default iterator) | TermIter |
Database | postlist(tname) | postlist_begin(tname) | PostingIter |
Database | termlist(docid) | termlist_begin(docid) | TermIter |
Database | positionlist(docid, tname) | positionlist_begin(docid, tname) | PositionIter |
Document | values() | values_begin() | ValueIter |
Document | termlist() | termlist_begin() (also as default iterator) | TermIter |
QueryParser | stoplist() | stoplist_begin() | TermIter |
QueryParser | unstemlist(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:
Class | Returns |
MSetIter | [docid, weight, rank, percentage, document] |
ESetIter | [termname, weight] |
TermIter | [term, wdf, termfreq, position iterator] |
PostingIter | [docid, doclength, wdf, position iterator] |
PositionIter | termpos |
ValueIter | [valueno, value] |
MSet objects have some additional methods to simplify access (these work using the C++ array dereferencing):
Method name | Explanation |
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.
Index | Contents |
xapian.MSET_DID | Document id |
xapian.MSET_WT | Weight |
xapian.MSET_RANK | Rank |
xapian.MSET_PERCENT | Percentage 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.
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.
Index | Contents |
xapian.ESET_TNAME | Term name |
xapian.ESET_WT | Weight |
Xapian::Auto::open_stub()
is wrapped as xapian.open_stub()
Xapian::Quartz::open()
is wrapped as xapian.quartz_open()
Xapian::InMemory::open()
is wrapped as xapian.inmemory_open()
Xapian::Remote::open()
is wrapped as xapian.remote_open()
(only
the TCP version is currently wrapped, the "program" version isn't).
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)])
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.
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 1Last updated $Date: 2006-10-10 16:04:08 +0100 (Tue, 10 Oct 2006) $