【问题标题】:Lucene.net high CPU usage while building indexLucene.net 构建索引时 CPU 使用率高
【发布时间】:2020-04-15 13:53:18
【问题描述】:

我编写了一个程序,它使用 Lucene.net 来索引一个 3GB 的文本文件。在构建索引时,进程的 CPU 消耗高达 80,内存使用量上升到 ~1GB。 有什么方法可以限制 CPU 和内存的使用? 下面是我用来构建索引的程序-

public void BuildIndex(string item)
        {
            System.Diagnostics.EventLog.WriteEntry("LuceneSearch", "Indexing Started for " + item);
            string indexPath = string.Format(BaseIndexPath, "20200414", item);
            if (System.IO.Directory.Exists(indexPath))
            {
                System.IO.Directory.Delete(indexPath, true);
            }


            LuceneIndexDirectory = FSDirectory.Open(indexPath);
            Writer = new IndexWriter(LuceneIndexDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);


            Writer.SetRAMBufferSizeMB(500);

            string file = "c:\LogFile.txt";
            string line=string.Empty;
            int count = 0;
            StreamReader fileReader = new StreamReader(file);
            while ((line = fileReader.ReadLine()) != null)
            {
                count++;
                Document doc = new Document();

                try
                {
                    doc.Add(new Field("LineNumber", count.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("LogTime", line.Substring(6, 12), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("LineText", line.Substring(18, line.Length -18 ), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    Writer.AddDocument(doc);
                }
                catch (Exception)
                {

                    System.Diagnostics.EventLog.WriteEntry("LuceneSearch", "Exception ocurred while entring a line in the index");
                }

            }
            System.Diagnostics.EventLog.WriteEntry("LuceneSearch", "Indexing finished for " + item + ". Starting Optimization now.");
            Writer.Optimize();
            Writer.Commit();

            Writer.Close();


            LuceneIndexDirectory.Dispose();

            System.Diagnostics.EventLog.WriteEntry("LuceneSearch", "Optimization finished for " + item );
        }

【问题讨论】:

    标签: c# lucene lucene.net


    【解决方案1】:

    编写索引通常是在搜索带外完成的。也就是说,它通常在部署或应用程序启动期间完成。当然,也可以进行近乎实时的搜索,包括保持打开的IndexWriter 用于写入和搜​​索相同的索引,但在这种情况下,典型的应用程序一次会添加几个文档,它不会一次构建整个索引。

    一般来说,如果您是在应用程序生命周期的正确时间点构建索引,那么使用这么多 RAM 并不是什么大问题。

    但是,您在调用 Optimize() 时不带任何参数,这会在您创建后重写整个索引。如果您的写入索引占用多个段,则不带参数调用 Optimize() 会将整个索引重写为单个段。

    来自文档(重点是我的):

    请求对索引进行“优化”操作,启动索引以获得最快的可用搜索。传统上,这意味着将所有段合并到一个段中,就像在默认合并策略中所做的那样,但是各个合并策略可以以不同的方式实现优化。

    建议在索引完成后调用该方法。在频繁更新的环境中,优化最好在少量时间(如果有的话)进行。

    更多讨论请见http://www.gossamer-threads.com/lists/lucene/java-dev/47895

    请注意,优化需要 2 倍索引大小的可用空间(如果您使用复合文件格式,则为 3 倍)。例如,如果您的索引大小为 10 MB,那么您需要 20 MB 可用空间来完成优化(如果您使用复合字段格式,则需要 30 MB)。

    如果在优化过程中重新打开部分但不是所有读取器,这将导致消耗超过 2 倍的临时空间,因为这些新读取器将在那时保持打开部分优化的段。优化运行时最好不要重新打开阅读器。

    请注意,Optimize() 方法在 Lucene 4.x 中已被删除(有充分的理由),所以我建议您现在停止使用它。

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多