我有一个文件夹(MY_FILES),有大约500个文件,每天有一个新文件到达,它放在那里.每个文件的大小约为4Mb.
我刚刚开发了一个简单的'void main'来测试我是否可以在这些文件中搜索特定的通配符.它工作得很好.
问题是我正在删除旧的indexed_folder并重新索引.这需要花费很多时间,显然效率低下.我正在寻找的是"增量索引".意思是,如果索引已经存在 - 只需将新文件添加到索引中.
我想知道Lucene是否有某种机制来检查'doc'是否在尝试索引之前被编入索引.像writer.isDocExists这样的东西?
谢谢!
我的代码看起来像这样:
// build the writer
IndexWriter writer;
IndexWriterConfig indexWriter = new IndexWriterConfig(Version.LUCENE_36, analyzer);
writer = new IndexWriter(fsDir, indexWriter);
writer.deleteAll(); //must - otherwise it will return duplicated result
//build the docs and add to writer
File dir = new File(MY_FILES);
File[] files = dir.listFiles();
int counter = 0;
for (File file : files)
{
String path = file.getCanonicalPath();
FileReader reader = new FileReader(file);
Document doc = new Document();
doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.ANALYZED)); …Run Code Online (Sandbox Code Playgroud) 在我的例子中,插入到lucene索引中的每个文档都有其唯一的ID.在lucene索引中添加新文档时,如果文档已存在于索引中,则不应将该文档插入到索引中.如何实施这一战略?
我想我应该首先使用docId搜索文档,如果lucene找不到文档,那么我插入它.但是,因为我有3个线程共享唯一的indexWriter索引,我想应该有一些错误的情况.例如:线程1和线程2正在处理具有相同docId的两个文档,如果thread1搜索docId并且什么都没找到,它会将文档插入到索引中,但是thread2可以在thread1读取索引之后将其文档插入索引.结果,索引中存在两个文档.我怎么能避免这个?