我正在使用 lucene 来索引文档并执行搜索,然后我立即删除它们.所有这些都可以被认为是一个有点原子的动作,包括以下步骤:
i'm using lucene to index documents and perform a search after which, i immediately delete them. all this can be considered as a somewhat atomic action that includes the following steps:
index (writer) --> search (searcher) --> 按分数获取文档(读者)-->删除文档(读者)
index (writer) --> search (searcher) --> get docs by score (reader) --> delete docs (reader)
这个动作可以由多个并发线程在同一个索引上执行(使用FSDirectory
).
this action can be performed by multiple concurrent threads on the same index (using FSDirectory
).
重要提示:每个线程处理一组单独的文档,因此一个线程不会接触另一个线程的文档
IMPORTANT NOTE: each thread handles a separate set of documents so one thread will not touch another thread's documents
为此,我有几个问题:
1) 我应该使用 IndexWriter
、IndexReader
和 IndexSearcher
的单个实例(用于所有线程)吗?(它们应该是线程安全的)
1) should i use a single instances (for all threads) of IndexWriter
, IndexReader
and IndexSearcher
? (they're supposed to be thread safe)
2) IndexWriter
可以在 IndexReader
删除文档时操作索引吗?我需要关闭一个让另一个做它的事情吗?意思是,一个线程可以写入索引而另一个线程从中删除(正如我之前提到的,我可以保证它们处理单独的数据集)
2) can an IndexWriter
manipulate an index while and IndexReader
deletes documents? do i need to close one for the other to do its thing?
meaning, can one thread write to an index while another one deletes from it (as i mentioned earlier, i can guarantee that they handle separate sets of data)
3) 您可能提出的任何其他良好做法和建议将不胜感激.
3) any other good practices and suggestions you might have will be most appreciated.
非常感谢!
IndexWriter
, IndexReader
和 IndexSearcher
是 根据 api javadoc 线程安全:
IndexWriter
, IndexReader
and IndexSearcher
are thread-safe according to the api javadoc:
注意:IndexSearcher 实例是完全线程安全的,这意味着多个线程可以同时调用它的任何方法
NOTE: IndexSearcher instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently
注意:IndexReader 实例是完全线程安全的,这意味着多个线程可以同时调用它的任何方法.
NOTE: IndexReader instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently.
注意:IndexWriter 实例是完全线程安全的,这意味着多个线程可以同时调用它的任何方法
NOTE: IndexWriter instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently
可以打开多个只读IndexReader
,但最好共享一个(出于性能原因).
Multiple read-only IndexReader
s can be opened, but it's better to share one (for performance reasons).
只能打开一个IndexWriter
(它会创建一个写锁来防止其他人在同一个索引上被打开).您可以使用 IndexReader
删除文档,而 IndexWriter
持有此锁.IndexReader
将始终看到打开时的索引,编写器所做的更改只有在编写器提交后才可见,阅读器重新打开.
Only a single IndexWriter
can be opened (and it will create a write lock to prevent others from being opened on the same index). You can use IndexReader
to delete documents while IndexWriter
holds this lock. IndexReader
will always see the index as it was at the time when it was opened, changes done by the writer will be visible only after the writer commits them the reader is reopened.
可以打开任意数量的 IndexSearcher
,但最好还是共享一个.即使在修改索引时也可以使用它们.与 IndexReader
的工作方式相同(在重新打开搜索器之前,更改不可见).
Any number of IndexSearcher
s can be opened, but again it's better to share one. They can be used even while the index is being modified. Works the same as for IndexReader
(the changes are not visible until the searcher is reopened).
这篇关于lucene 良好实践和线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!