我们有一个程序,该程序可以连续运行,执行各种操作并更改数据库中的某些记录。这些记录使用Lucene进行索引。因此,每次更改实体时,我们都会执行以下操作:
indexWriter.deleteDocuments(..)
和indexWriter.addDocument(..)
在Lucene中更新该实体。 这可以正常工作,但是随着时间的流逝,
indexWriter.commit()
需要越来越多的时间。最初大约需要0.5秒,但经过几百次这样的事务后,则需要3秒以上。我毫不怀疑,如果脚本运行更长的时间,甚至会花费更长的时间。到目前为止,我的解决方案是注释掉
indexWriter.addDocument(..)
和indexWriter.commit()
,并不时地重新创建整个索引,方法是首先使用indexWriter.deleteAll()
然后在一个Lucene transction/IndexWriter内重新添加所有文档(在约14秒内约25万个文档)。但这显然违背了数据库和Lucene提供的事务处理方法,后者使两者保持同步,并使对数据库的更新对于使用Lucene搜索的我们工具的用户可见。我可以在14秒内添加25万个文档,但是添加1个文档需要3秒似乎很奇怪。我做错了什么,该如何改善呢?
最佳答案
您做错了,是假设Lucene的built-in transactional capabilities具有性能,并且保证与典型的关系数据库they really don't相当。更具体地讲,提交将所有索引文件与磁盘同步,使提交时间与索引大小成正比。这就是为什么随着时间的流逝,您的indexWriter.commit()
需要越来越多的时间的原因。 IndexWriter.commit()
的Javadoc甚至警告:
您能想象数据库文档会告诉您避免进行提交吗?
由于您的主要目标似乎是通过Lucene搜索及时保持数据库更新可见,以改善这种情况,请执行以下操作:
indexWriter.deleteDocuments(..)
和indexWriter.addDocument(..)
触发器indexWriter.commit()
而不是每次事务,以确保最终将所做的更改写入磁盘SearcherManager
进行搜索并定期调用 maybeRefresh()
以在合理的时间范围内查看更新的文档以下是一个示例程序,该程序演示了如何通过定期执行
maybeRefresh()
来检索文档更新。它建立一个包含100000个文档的索引,使用 ScheduledExecutorService
设置commit()
和maybeRefresh()
的定期调用,提示您更新单个文档,然后反复搜索直到更新可见。程序终止时,将正确清理所有资源。注意,更新何时可见的控制因素是当调用maybeRefresh()
时,而不是commit()
。import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.concurrent.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
public class LucenePeriodicCommitRefreshExample {
ScheduledExecutorService scheduledExecutor;
MyIndexer indexer;
MySearcher searcher;
void init() throws IOException {
scheduledExecutor = Executors.newScheduledThreadPool(3);
indexer = new MyIndexer();
indexer.init();
searcher = new MySearcher(indexer.indexWriter);
searcher.init();
}
void destroy() throws IOException {
searcher.destroy();
indexer.destroy();
scheduledExecutor.shutdown();
}
class MyIndexer {
IndexWriter indexWriter;
Future commitFuture;
void init() throws IOException {
indexWriter = new IndexWriter(FSDirectory.open(Paths.get("C:\\Temp\\lucene-example")), new IndexWriterConfig(new StandardAnalyzer()));
indexWriter.deleteAll();
for (int i = 1; i <= 100000; i++) {
add(String.valueOf(i), "whatever " + i);
}
indexWriter.commit();
commitFuture = scheduledExecutor.scheduleWithFixedDelay(() -> {
try {
indexWriter.commit();
} catch (IOException e) {
e.printStackTrace();
}
}, 5, 5, TimeUnit.MINUTES);
}
void add(String id, String text) throws IOException {
Document doc = new Document();
doc.add(new StringField("id", id, Field.Store.YES));
doc.add(new StringField("text", text, Field.Store.YES));
indexWriter.addDocument(doc);
}
void update(String id, String text) throws IOException {
indexWriter.deleteDocuments(new Term("id", id));
add(id, text);
}
void destroy() throws IOException {
commitFuture.cancel(false);
indexWriter.close();
}
}
class MySearcher {
IndexWriter indexWriter;
SearcherManager searcherManager;
Future maybeRefreshFuture;
public MySearcher(IndexWriter indexWriter) {
this.indexWriter = indexWriter;
}
void init() throws IOException {
searcherManager = new SearcherManager(indexWriter, true, null);
maybeRefreshFuture = scheduledExecutor.scheduleWithFixedDelay(() -> {
try {
searcherManager.maybeRefresh();
} catch (IOException e) {
e.printStackTrace();
}
}, 0, 5, TimeUnit.SECONDS);
}
String findText(String id) throws IOException {
IndexSearcher searcher = null;
try {
searcher = searcherManager.acquire();
TopDocs topDocs = searcher.search(new TermQuery(new Term("id", id)), 1);
return searcher.doc(topDocs.scoreDocs[0].doc).getField("text").stringValue();
} finally {
if (searcher != null) {
searcherManager.release(searcher);
}
}
}
void destroy() throws IOException {
maybeRefreshFuture.cancel(false);
searcherManager.close();
}
}
public static void main(String[] args) throws IOException {
LucenePeriodicCommitRefreshExample example = new LucenePeriodicCommitRefreshExample();
example.init();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
example.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
});
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a document id to update (from 1 to 100000): ");
String id = scanner.nextLine();
System.out.print("Enter what you want the document text to be: ");
String text = scanner.nextLine();
example.indexer.update(id, text);
long startTime = System.nanoTime();
String foundText;
do {
foundText = example.searcher.findText(id);
} while (!text.equals(foundText));
long elapsedTimeMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
System.out.format("it took %d milliseconds for the searcher to see that document %s is now '%s'\n", elapsedTimeMillis, id, text);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
该示例已使用Lucene 5.3.1和JDK 1.8.0_66成功进行了测试。