devesh
devesh

Reputation:

How do I store the lucene index in a database?

This is my sample code:

MysqlDataSource dataSource = new MysqlDataSource();

dataSource.setUser("root");
dataSource.setPassword("ncl");
dataSource.setDatabaseName("userdb");
dataSource.setEmulateLocators(true); //This is important because we are dealing with a blob type data field
try{    
    JdbcDirectory jdbcDir = new JdbcDirectory(dataSource, new MySQLDialect(), "tttable");
    StandardAnalyzer analyzer = new StandardAnalyzer();
    IndexWriter writer = new IndexWriter(jdbcDir, analyzer,false);
    writer.optimize();
    writer.close();
}catch(Exception e){
System.out.print(e);
}

I am stuck at this line: IndexWriter writer = new IndexWriter(jdbcDir, analyzer,false);

Everytime I try to run this code, I receive the following exception:

------"org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out: PhantomReadLock[write.lock/tttable]"------------

I cannot find what is wrong with the code. It may be it is a jar compatibility issue.

I am unable to get an IndexWriter object.

Upvotes: 3

Views: 3186

Answers (2)

Andrew Gans
Andrew Gans

Reputation: 750

You should create table first.

Try to use such a code:

if (!dir.tableExists())
    {
        dir.create();
    }

Locks are made by IndexWriter. If something is wrong lock is not released, so you need to release all locks before create new writer (there is static method of IndexWriter class)

IndexWriter.unlock(dir);

Upvotes: 0

itsadok
itsadok

Reputation: 29342

It seems like the index is locked. If you're sure it shouldn't be locked, then maybe some process crashed without proper cleanup.

Try adding the line

jdbcDir.clearLock();

before creating the indexWriter.

Don't leave it there, tough. You generally want to let Lucene manage the locks, to disallow two IndexWriters from writing to the same index.

Upvotes: 4

Related Questions