How to save lucene index in 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 on this line: IndexWriter writer = new IndexWriter(jdbcDir, analyzer,false);

Every time I try to run this code, I get the following exception:

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

I can not find what is wrong with the code. Perhaps this is a compatibility problem with banks.

I cannot get an IndexWriter object.

+3
source share
2 answers

It seems that the index is locked. If you are sure that it should not be blocked, some process may have crashed without proper cleaning.

Try adding a line

jdbcDir.clearLock();

before creating indexWriter.

, . , Lucene , IndexWriters .

+4

.

:

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

IndexWriter. - , , ( IndexWriter)

IndexWriter.unlock(dir);
0

Source: https://habr.com/ru/post/1702677/


All Articles