【问题标题】:adding documents to an existing index in lucene将文档添加到 lucene 中的现有索引
【发布时间】:2010-10-27 13:29:38
【问题描述】:

我想问一下如何将新文档添加到现有的 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);
}
} 

【问题讨论】:

    标签: java lucene


    【解决方案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

    【讨论】:

      【解决方案2】:

      还要添加我应该使用的新文档 …… 但我的问题是如何将新文档添加到现有的 lucene 索引中

      你能澄清一下你的意思吗?正如您所说,您知道如何将文档添加到索引中,但是您问如何...添加新文档?

      【讨论】:

      • 好吧,这是我的错误。我没有完全理解源代码。但是在阅读您的评论后我才意识到它。感谢您的提示。 :-)
      • 问题是如何将新文档添加到现有索引,而不是如何在初始索引期间简单地添加新文档。
      【解决方案3】:

      当您实例化一个新的IndexWriter 时,您不会创建一个新索引(除非您明确告诉 lucene 强制创建一个新索引)。因此,无论索引是否已经存在,您的代码都可以正常工作。

      【讨论】:

      • 是的,我知道。但我试图将新文档添加到现有索引中。您认为我应该怎么做才能实现这一目标? :-)
      • 那我不明白你的问题。您创建一个查看现有索引的索引器,其方式与创建创建新索引的索引器完全相同。因此,无论indexDir 中是否包含内容,您的代码都可以正常工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多