简体   繁体   English

在Lucene中并发读写

[英]Concurrent Read & Write in Lucene

I have an application that needs to be able to read and write a search index concurrently. 我有一个需要能够同时读取和写入搜索索引的应用程序。 What I noticed with lucene is that you can't use IndexWriter and DirectoryReader concurrently. 我使用lucene注意到,您不能同时使用IndexWriter和DirectoryReader。 Basically: 基本上:

IndexWriter writer = new IndexWriter(directory, config);
//Add documents here...
writer.commit();

DirectoryReader reader = DirectoryReader.open(writer.getDirectory());
IndexSearcher searcher = new IndexSearcher(reader);     
QueryParser qp = new QueryParser(Version.LUCENE_46,"field", new StandardAnalyzer(Version.LUCENE_46));
qp.setAllowLeadingWildcard(true);
Query q = qp.parse("field:*");

works fine, while 工作正常,而

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new StandardAnalyzer(Version.LUCENE_46));
IndexWriter writer = new IndexWriter(directory, config);
writer.commit();
DirectoryReader reader = DirectoryReader.open(writer.getDirectory());

//Add documents here

writer.commit();
IndexSearcher searcher = new IndexSearcher(reader);     
QueryParser qp = new QueryParser(Version.LUCENE_46,"field", new StandardAnalyzer(Version.LUCENE_46));
qp.setAllowLeadingWildcard(true);
Query q = qp.parse("field:*");

doesn't work at all. 根本不起作用。

Do I have to reopen the DirectoryReader after each commit? 每次提交后都必须重新打开DirectoryReader吗?

As we could see the main difference between two snippies is the time u got a reader from the IndexWriter , first one is after the writer.commit() . 我们可以看到,两个片段之间的主要区别是您从IndexWriter获得读者的时间,第一个是在writer.commit()


when we got a reader ,that means we've got a one-time-snapshot of the current Index file, the second reader got a "snapshot" before the IndexWriter.commit , it will turn out that the IndexSearcher based on that reader won't find changes IndexWriter just made or the Index isn't in a consistent state . 当我们有一个读者时,这意味着我们已经获得了当前Index文件的一次性快照,第二个readerIndexWriter.commit之前获得了一个“快照”,事实证明基于该readerIndexSearcher赢得了找不到IndexWriter刚刚进行的更改,或者索引未处于一致状态。

so u should reopen the DirectoryReader . 因此,您应该重新打开DirectoryReader。 hope these will do some help ! :D 希望这些会有所帮助!:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM