[Xapian-discuss] Creating or Opening a Database

Iain Emsley iainemsley at googlemail.com
Mon Jun 2 21:43:35 BST 2008


Just starting out with Xapian and the Python bindings but I've not been able
to get my code to open or create a database file to index the text using the
simple index as a start. I'd be grateful for any advice on what I've done
incorrectly in the _db code:
import xapian
import string

class TextIndex (object):
 _db = None
def get_db ():
    if _db == None:
     _db = xapian.WritableDatabase ('c:\\index', xapian.DB_CREATE_OR_OPEN)
    return _db

def index (db):
#need to add in time taken to index
  try:

    indexer = xapian.TermGenerator()
    stemmer = xapian.Stem("english")
    indexer.set_stemmer(stemmer)
    texts = open('C:\\webroot\\milton\\miltontest.txt')

    para = ''
    try:
        for line in texts:
            line = string.strip(line)
            if line == '':
                if para != '':
                    # We've reached the end of a paragraph, so index it.
                    doc = xapian.Document()
                    doc.set_data(para)

                    indexer.set_document(doc)
                    indexer.index_text(para)

                    # Add the document to the database.
                    get_db().add_document(doc)
                    para = ''
            else:
                if para != '':
                    para += ' '
                para += line
    except StopIteration:
        pass

  except Exception, e:
       print >> sys.stderr, "Exception: %s" % str(e)


More information about the Xapian-discuss mailing list