gpt4 book ai didi

c# - 如何提高 Lucene.net 索引速度

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:08 31 4
gpt4 key购买 nike

我正在使用 lucene.net 来索引我的 pdf 文件。索引 15000 个 pdf 大约需要 40 分钟,索引时间随着我文件夹中 pdf 文件数量的增加而增加。

  • 如何提高 lucene.net 中的索引速度?
  • 是否有其他索引服务具有快速索引性能?

我正在使用最新版本的 lucene.net 索引 (Lucene.net 3.0.3)。

这是我的索引代码。

public void refreshIndexes() 
{
// Create Index Writer
string strIndexDir = @"E:\LuceneTest\index";
IndexWriter writer = new IndexWriter(Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir)), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, IndexWriter.MaxFieldLength.UNLIMITED);

// Find all files in root folder create index on them
List<string> lstFiles = searchFiles(@"E:\LuceneTest\PDFs");
foreach (string strFile in lstFiles)
{
Document doc = new Document();
string FileName = System.IO.Path.GetFileNameWithoutExtension(strFile);
string Text = ExtractTextFromPdf(strFile);
string Path = strFile;
string ModifiedDate = Convert.ToString(File.GetLastWriteTime(strFile));
string DocumentType = string.Empty;
string Vault = string.Empty;

string headerText = Text.Substring(0, Text.Length < 150 ? Text.Length : 150);
foreach (var docs in ltDocumentTypes)
{
if (headerText.ToUpper().Contains(docs.searchText.ToUpper()))
{
DocumentType = docs.DocumentType;
Vault = docs.VaultName; ;
}
}

if (string.IsNullOrEmpty(DocumentType))
{
DocumentType = "Default";
Vault = "Default";
}

doc.Add(new Field("filename", FileName, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("text", Text, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("path", Path, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("modifieddate", ModifiedDate, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("documenttype", DocumentType, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("vault", Vault, Field.Store.YES, Field.Index.ANALYZED));

writer.AddDocument(doc);
}
writer.Optimize();
writer.Dispose();
}

最佳答案

索引部分看起来不错。请注意,IndexWriter 是线程安全的,因此使用 Parallel.Foreach(将 MaxConcurrency 设置为核心数。使用此值)可能会在您使用多核计算机时有所帮助。

但是您的 GC 因文档类型检测部分而变得疯狂。所有 ToUpper() 都是痛苦的。

  • 在 lstFiles 循环之外。以大写形式创建 ltDocumentTypes .searchText 的副本

    var upperDocTypes = ltDocumentTypes.Select(x=>x.searchText.ToUpper()).ToList();
  • 在文档类型循环之外创建另一个字符串

    string headerTestUpper = headerText.ToUpper();
  • 当它找到匹配时“中断”。一旦找到匹配项,这将退出循环并阻止所有后续迭代。当然,这意味着首先匹配,而你的是最后匹配(如果这对你有影响的话)

    string headerText = Text.Substring(0, Text.Length < 150 ? Text.Length : 150);
    foreach (var searchText in upperDocTypes)
    {
    if (headerTextUpper.Contains(searchText))
    {
    DocumentType = docs.DocumentType;
    Vault = docs.VaultName;
    break;
    }
    }

根据 ltDocumentTypes 的大小,这可能不会给您带来太大的改进。

我敢打赌,ExtractTextFromPdf 是最昂贵的部分。通过分析器运行此程序或使用一些秒表进行检测将使您了解成本所在。

关于c# - 如何提高 Lucene.net 索引速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38670879/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com