c# - Lucene IndexWriter 添加文件慢

标签 c# lucene lucene.net

我写了一个小循环,将 10,000 个文档添加到 IndexWriter 中,这花了很长时间。

还有其他方法可以索引大量文档吗?

我问是因为当它上线时它必须加载 15,000 条记录。

另一个问题是如何避免在重新启动 Web 应用程序时再次加载所有记录?

编辑

这是我使用的代码;

for (int t = 0; t < 10000; t++){
    doc = new Document();
    text = "Value" + t.toString();
    doc.Add(new Field("Value", text, Field.Store.YES, Field.Index.TOKENIZED));
    iwriter.AddDocument(doc);
};

编辑2

        Analyzer analyzer = new StandardAnalyzer();
        Directory directory = new RAMDirectory();

        IndexWriter iwriter = new IndexWriter(directory, analyzer, true);

        iwriter.SetMaxFieldLength(25000);

然后是添加文件的代码,然后;

        iwriter.Close();

最佳答案

您应该这样做以获得最佳性能。在我的机器上,我在 1 秒内索引了 1000 个文档

1) 你应该重用 (Document, Field) 而不是每次添加这样的文档时都创建

private static void IndexingThread(object contextObj)
{
     Range<int> range = (Range<int>)contextObj;
     Document newDoc = new Document();
     newDoc.Add(new Field("title", "", Field.Store.NO, Field.Index.ANALYZED));
     newDoc.Add(new Field("body", "", Field.Store.NO, Field.Index.ANALYZED));
     newDoc.Add(new Field("newsdate", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
     newDoc.Add(new Field("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

     for (int counter = range.Start; counter <= range.End; counter++)
     {
         newDoc.GetField("title").SetValue(Entities[counter].Title);
         newDoc.GetField("body").SetValue(Entities[counter].Body);
         newDoc.GetField("newsdate").SetValue(Entities[counter].NewsDate);
         newDoc.GetField("id").SetValue(Entities[counter].ID.ToString());

         writer.AddDocument(newDoc);
     }
}

之后您可以使用线程并将您的大集合分成较小的集合,并为每个部分使用上面的代码 例如,如果您有 10,000 个文档,您可以使用 ThreadPool 创建 10 个线程并将每个部分提供给 一个索引线程

那么您将获得最佳性能。

关于c# - Lucene IndexWriter 添加文件慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3295834/

相关文章:

c# - 从 IWebElement 获取 WebDriver

c# - 一种方法,多种列表类型

security - Solr 架构更改不会丢失数据且无需使用动态字段

java - 使用 BooleanQuery 还是编写更多索引?

solr - 使用 ravenDB 进行空间搜索

javascript - 如何为多种语言生成类

c# - 在布局 View 上填充 ViewBag

spring - @IndexedEmbedded 在延迟加载的实体列表上,不会自动进入搜索索引

hadoop - 如何显示Hdfs目录路径通过solr搜索作为输出?假设如果用户在solr中搜索,那么我想显示该目录的路径?

c# - 如何在 lucene.net 中使用函数 SynonymAnalyzer