如何将 lucene 索引存储到数据库中?
这是我的示例代码:
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);
}
我被困在这一行:IndexWriter writer = new IndexWriter(jdbcDir,analyzer,false);
每次我尝试运行此代码时,都会收到以下异常:
------“org.apache.lucene.store.LockObtainFailedException: 锁获取超时: PhantomReadLock[write.lock/tttable]"------------
我找不到代码有什么问题。可能是 jar 兼容性问题。
我无法获取 IndexWriter 对象。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好像索引被锁定了。 如果您确定它不应该被锁定,那么可能某些进程在没有适当清理的情况下崩溃了。
尝试
在创建 indexWriter 之前添加该行。
别把它留在那里,强硬。 您通常希望让 Lucene 管理锁,以禁止两个 IndexWriter 写入同一索引。
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
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.
您应该先创建表。
尝试使用这样的代码:
由IndexWriter进行锁定。 如果出现错误,锁不会被释放,所以你需要在创建新的 writer 之前释放所有锁(有 IndexWriter 类的静态方法)
You should create table first.
Try to use such a code:
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)