2010-10-27 40 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
相关问题