I want to use xapian as a full text search engine. However, when I initialize it with the following method, I see no errors, but nothing writes to the database. If I copy all of the code into view, it works. However, this is not an option since I can only use a single connection for writing to the database, so it needs to be shared amongst all threads. I would like to initialize it in __init__.py, but I must be doing something wrong.
I added this code to models.py:
import xapian
class XapianDB(object):
def __init__(self):
self.indexer = xapian.TermGenerator()
stemmer = xapian.Stem("english")
self.indexer.set_stemmer(stemmer)
def connect(self, xapdb_path):
self.xapdb = xapian.WritableDatabase(xapdb_path, xapian.DB_CREATE_OR_OPEN)
def add_startup(self, startup):
doc = xapian.Document()
self.indexer.set_document(doc)
self.indexer.index_text(startup.name, 1, 'S')
self.indexer.index_text(startup.headquarters, 1, 'XH')
self.indexer.index_text(startup.name)
self.indexer.increase_termpos()
self.indexer.index_text(startup.headquarters)
doc.set_data("look at all of this data")
idterm = "Q" + str(startup.id)
doc.add_boolean_term(idterm)
self.xapdb.replace_document(idterm, doc)
xapp = XapianDB()
Then in __init__.py I added
from .models import xapp
def main(global_config, **settings):
#... other code
xapp.connect("/path/to/xapiandb")
And I make a silly view:
from .models import xapp
class ViewFrontpage(ViewWarlock):
def __init__(self, context, request):
ViewWarlock.__init__(self, context, request)
@view_config(route_name='test3', renderer='templates/test2.jinja2')
def test3(self):
startup.name = "a name"
startup.long_info = "we will always be there doing the best that we can do for the people we care about"
startup.id = 100
startup.headquarters = "Gainesville, Georgia"
xapp.add_startup(startup)
return {'gibs': self.gibs,
}