lucene.net索引速度下降

Far*_*ker 0 .net c# lucene lucene.net

我正在使用Lucene.net搜索大约50K实体.这些实体保存在数据库中.我创建了一个应用程序,它试图在每次索引100个实体.

代码很简单:

var entityList = GetEntityList(100);

foreach (var item in entityList) 
    Indexer.IndexEntity(item);
Run Code Online (Sandbox Code Playgroud)

这是Indexer类:

public class Indexer {
    public void IndexEntity(Entity item)
    {
        IndexWriter writer;
        string path = ConfigurationManager.AppSettings["SearchIndexPath"];
        FSDirectory directory = FSDirectory.Open(new DirectoryInfo(path));
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
        if (Directory.GetFiles(path).Length > 0)
            writer = new IndexWriter(directory, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
        else
            writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
        Document document = new Document();
        document.Add(new Field("id", item.Id.ToString(), Field.Store.YES, Field.Index.NO, Field.TermVector.NO));
        document.Add(new Field("category", item.Category.Id.ToString(), Field.Store.YES, Field.Index.NO, Field.TermVector.NO));
        document.Add(new Field("location", item.Location.Id.ToString(), Field.Store.YES, Field.Index.NO, Field.TermVector.NO));
        document.Add(new Field("point", item.Point.ToString(), Field.Store.YES, Field.Index.NO, Field.TermVector.NO));
        document.Add(new Field("picture", item.PictureUrl, Field.Store.YES, Field.Index.NO, Field.TermVector.NO));
        document.Add(new Field("creationdate", item.CreationDate.ToString(), Field.Store.YES, Field.Index.NO, Field.TermVector.NO));
        document.Add(new Field("title", item.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
        document.Add(new Field("body", item.Body, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
        string str2 = string.Empty;
        foreach (Tag tag in item.Tags)
        {
            if (!string.IsNullOrEmpty(str2))
            {
                str2 = str2 + "-";
            }
            str2 = str2 + tag.DisplayName;
        }
        document.Add(new Field("tags", str2, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
        writer.AddDocument(document);
        writer.Optimize();
        writer.Close();
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,我的搜索速度现在足够好了.但问题是索引速度降低了.到目前为止,我的应用程序已被索引约15K实体,索引文件大小约为600MB.现在,当它想要为100个新实体编制索引时,需要大约24分钟!

问题是什么?提前致谢.

Adr*_*lon 5

在您的代码中,有两件事情非常明显:

  1. 您在添加每个文档后优化索引.对于Lucene的最新版本,有很好的理由为什么你根本不应该优化你的索引(每段缓存),尽管有这些原因,在添加每个文档后优化你的索引是疯狂的过度杀伤力
  2. 您不断打开/关闭/提交索引.给定循环结构,为什么不在循环外打开索引编写器,添加实体,然后关闭/提交.如果你需要更快的索引可见性,你可以在循环中添加一个定期的提交命令(基于某种模数算术听起来对我来说是好的.

有了这两个变化,我认为你会看到你的索引工作急剧加速.