2010-10-27 173 views
3

我想問如何將新文檔添加到現有的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); 
} 
} 

回答

2

還添加新的文件,我應該使用 .... 但我的問題是究竟如何可以添加新的文件到現有的Lucene索引

可以請你澄清你是什麼意思?如您所述,您知道如何將文檔添加到索引中,但是您會問如何......添加新文檔?

+0

好的,這是我的錯。我沒有完全理解源代碼。但在閱讀你的評論後,我才意識到它。然後感謝提示。 :-) – jacobian 2010-10-29 08:26:10

1

當你實例化一個新的IndexWriter,你將不會創建新的索引(除非你明確地告訴Lucene來迫使一個新的)。因此,無論索引是否已存在,您的代碼都可以正常工作。

+0

是的我知道,但我試圖添加新的文件到現有的索引。你認爲我應該怎麼做到這一點? :-) – jacobian 2010-10-27 17:06:41

+1

我不明白你的問題。您創建一個索引編寫器來查看現有索引,其方式與您創建一個索引編寫器以創建新索引的方式完全相同。所以無論indexDir是否有東西,你的代碼都可以工作。 – Xodarap 2010-10-27 17:15:23

+0

哦,是的,我只是意識到它,儘管如此。 :-) – jacobian 2010-10-29 07:24:03

1

基於Lucene API,當您構建IndexWriter時,構造函數允許您指定IndexWriterConfig

IndexWriter(Directory d, IndexWriterConfig conf) 

IndexWriterConfig允許您指定的開放模式:

IndexWriterConfig conf = new IndexWriterConfig(analyzer); 
conf.setOpenMode(IndexWriterConfig.OpenMode.APPEND); 

而且你有3種選擇:

  • IndexWriterConfig.OpenMode.APPEND
  • IndexWriterConfig.OpenMode.CREATE
  • IndexWriterConfig.OpenMode.CREATE_OR_APPEND
相關問題