我想问一下如何将新文档添加到现有的lucene中 指数。在下面的源代码中,我只是将IndexWriter的参数更改为false。
IndexWriter indexWriter = new IndexWriter(
FSDirectory.open(indexDir),
new SimpleAnalyzer(),
false,
IndexWriter.MaxFieldLength.LIMITED);
因为false意味着索引仍然是打开而不是关闭。还要添加我应该使用的新文件
indexWriter.addDocument(doc)
但我的问题是如何将新文档添加到现有的lucene索引中。我找不到在lucene类中放置包含新文档的新路径目录的位置,以便lucene可以索引这些新文档并将其添加到现有索引中。任何帮助将不胜感激。 感谢。
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class testlucene1 {
public static void main(String[] args) throws Exception {
File indexDir = new File("C:/Users/Raden/Documents/lucene/LuceneHibernate/adi");
File dataDir = new File("C:/Users/Raden/Documents/lucene/LuceneHibernate/adi");
String suffix = "txt";
testlucene1 indexer = new testlucene1();
int numIndex = indexer.index(indexDir, dataDir, suffix);
System.out.println("Total files indexed " + numIndex);
}
private int index(File indexDir, File dataDir, String suffix) throws Exception {
IndexWriter indexWriter = new IndexWriter(
FSDirectory.open(indexDir),
new SimpleAnalyzer(),
false,
IndexWriter.MaxFieldLength.LIMITED);
indexWriter.setUseCompoundFile(false);
indexDirectory(indexWriter, dataDir, suffix);
int numIndexed = indexWriter.maxDoc();
indexWriter.optimize();
indexWriter.close();
return numIndexed;
}
private void indexDirectory(IndexWriter indexWriter, File dataDir, String suffix) throws IOException {
File[] files = dataDir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
indexDirectory(indexWriter, f, suffix);
} else {
indexFileWithIndexWriter(indexWriter, f, suffix);
}
}
}
private void indexFileWithIndexWriter(IndexWriter indexWriter, File f, String suffix) throws IOException {
if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) {
return;
}
if (suffix != null && !f.getName().endsWith(suffix)) {
return;
}
System.out.println("Indexing file " + f.getCanonicalPath());
Document doc = new Document();
doc.add(new Field("contents", new FileReader(f)));
doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
indexWriter.addDocument(doc);
}
}
答案 0 :(得分:2)
你能说清楚你的意思吗?你知道如何将文档添加到索引中,但是你会问如何...添加新文档?还要添加我应该使用的新文档 .... 但我的问题是如何将新文档添加到现有的lucene索引
答案 1 :(得分:1)
当您实例化新的IndexWriter
时,您将不会创建新索引(除非您明确告诉lucene强制新的索引)。因此,无论索引是否已存在,您的代码都将有效。
答案 2 :(得分:1)
基于Lucene API,在构建IndexWriter
时,构造函数允许您指定IndexWriterConfig
。
IndexWriter(Directory d, IndexWriterConfig conf)
IndexWriterConfig
允许您指定开放模式:
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
conf.setOpenMode(IndexWriterConfig.OpenMode.APPEND);
你有3个选择: