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.
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_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.
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).
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.
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 mymatcher(xapian.MatchDecider): def __init__(self): xapian.MatchDecider.__init__(self) def __call__(self, doc): return 1Last updated $Date: 2004-12-08 16:14:33 +0000 (Wed, 08 Dec 2004) $