-
Michel's Doctest teaser
last modified March 20, 2007 by whitmo
This is a doctest. You can execute this test using the python
'doctest' module. See run_tests.py
Xappa is a python search engine framework built with Xapian and
Twisted. The twisted bits are optional.
Xappa has the following cool features:
- Reusable. Core Python-only library can be easily reused by other
applications.
- Client/Server. Twisted networking server and client provides fast
multi-threaded Xapian database reading and writing.
- Thin. Not many new concepts beyond what Xapian already has.
- Scriptable. Orieted toward quick scripting and few hurdles to leap
to start indexing content.
- Queryable. Built-in dirt simple query language means no need to
build complex query object structures.
First, let's import some stuff we'll need:
>>> from xappa import util, index, xaql, time
>>> from xapp.doc import Document, TextField, Text, Data, Keyword
And then create a temporary Xapian database:
>>> db = util.tmp_db()
>>> type(db)
Documents can be added to the database by creating Xapdoc.Document
objects, assigning them field attributes, and then adding them to the
database.
>>> d1 = Document()
>>> d1.title = TextField('Foos and other Bars')
>>> d1.body = Text('Programmers often use foos and bars in their important code.')
>>> d1.status = Keyword('expired')
>>> d1.date = Data(time.time())
>>> d1.label = Data("Document1")
>>> d2 = Document()
>>> d2.title = TextField('Wings, Dings, and other Things')
>>> d2.body = Text('Chicken wings, dings dongs, and other meat derived delicacies.')
>>> d2.status = Keyword('active')
>>> d2.date = Data(time.time())
>>> d2.label = Data("Document2")
>>> d3 = Document()
>>> d3.title = TextField('Sloppy Joes, Moes, and Does')
>>> d3.body = Text('Dudes, deers, and the ground meat connection.')
>>> d3.status = Keyword('active')
>>> d3.date = Data(time.time())
>>> d3.label = Data("Document3")
Xappa Document objects have a 'xap' method that generates and
returns a Xapian document object. This generated object is what
actually gets added to the db:
>>> db.add_document(d1.xap())
1
>>> db.add_document(d2.xap())
2
>>> db.add_document(d3.xap())
3
Now that the database has documents in it, you can use either Xapian's
query API, or the xaql query language:
>>> xaql.pprints(xaql.query_xapian(db, "select label where title=bars"))
'label \nDocument1 \n'
>>> xaql.pprints(xaql.query_xapian(db, "select label where status=active and 'meat'"))
'label \nDocument3 \nDocument2 \n'
>>> xaql.pprints(xaql.query_xapian(db, "select label where status=expired"))
'label \nDocument1 \n'