gpt4 book ai didi

java - lucene:如何执行增量索引并避免 'delete and redo'

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:30:22 25 4
gpt4 key购买 nike

我有一个文件夹 (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));
doc.add(new Field("path", path, Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("content", reader));

writer.addDocument(doc);
System.out.println("indexing "+file.getName()+" "+ ++counter+"/"+files.length);
}

最佳答案

首先,您应该使用 IndexWriter.updateDocument(Term, Document) 而不是 IndexWriter.addDocument 来更新文档,这将防止您的索引包含重复条目。

要执行增量索引,您应该将last-modified 时间戳添加到索引的文档中,并且只索引较新的文档。

编辑有关增量索引的更多详细信息

您的文档应该至少有两个字段:

  • the path of the file
  • the time stamp when the file has been modified for the last time.

在开始索引之前,只需在您的索引中搜索最新的时间戳,然后爬取您的目录以查找时间戳比索引的最新时间戳更新的所有文件。

这样,每次文件更改时都会更新您的索引。

关于java - lucene:如何执行增量索引并避免 'delete and redo',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12462652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com