我想問如何將新文檔添加到現有的lucene 索引。在下面的源代碼中,我只是將IndexWriter的參數更改爲false。將文檔添加到lucene中的現有索引
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);
}
}
好的,這是我的錯。我沒有完全理解源代碼。但在閱讀你的評論後,我才意識到它。然後感謝提示。 :-) – jacobian 2010-10-29 08:26:10