如何将 lucene 索引存储到数据库中?

发布于 2024-07-12 23:28:03 字数 959 浏览 7 评论 0原文

这是我的示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾其所爱 2024-07-19 23:28:03

好像索引被锁定了。 如果您确定它不应该被锁定,那么可能某些进程在没有适当清理的情况下崩溃了。

尝试

jdbcDir.clearLock();

在创建 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

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.

轮廓§ 2024-07-19 23:28:03

您应该先创建表。

尝试使用这样的代码:

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

由IndexWriter进行锁定。 如果出现错误,锁不会被释放,所以你需要在创建新的 writer 之前释放所有锁(有 IndexWriter 类的静态方法)

IndexWriter.unlock(dir);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文