gpt4 book ai didi

c# - 有趣的 Lucene.net 异常

转载 作者:可可西里 更新时间:2023-11-01 09:14:24 25 4
gpt4 key购买 nike

根据 thisthis ,我通过多个线程使用相同的索引搜索器。但是当我从 FsDirectory 切换到 MMapDirectory 时,我遇到了有趣的异常。

这个工作正常:

static void Main(string[] args) 
{
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Tams\Desktop\new\");
var directory = FSDirectory.Open(directoryInfo);
var indexSearcher = new IndexSearcher(directory);

const int times = 100;
const int concurrentTaskCount = 5;
var task = new Task[concurrentTaskCount];
for (int i = 0; i < concurrentTaskCount; i++)
{
task[i] = new Task(() => Search(indexSearcher, times));
task[i].Start();
}

Task.WaitAll(task);
}

static void Search(IndexSearcher reader, int times)
{
List<Document> docs = new List<Document>(10000);
for (int i = 0; i < times; i++)
{
var q = new TermQuery(new Term("title", "volume"));
foreach (var scoreDoc in reader.Search(q, 100).ScoreDocs)
{
docs.Add(reader.Doc(scoreDoc.Doc));
}
}
}

但是有了这个:

static void Main(string[] args)
{
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Tams\Desktop\new\");
var directory = new MMapDirectory(directoryInfo); // CHANGED
var indexSearcher = new IndexSearcher(directory);

const int times = 100;
const int concurrentTaskCount = 5;
var task = new Task[concurrentTaskCount];
for (int i = 0; i < concurrentTaskCount; i++)
{
task[i] = new Task(() => Search(indexSearcher, times));
task[i].Start();
}

Task.WaitAll(task);
}

static void Search(IndexSearcher reader, int times)
{
List<Document> docs = new List<Document>(10000);
for (int i = 0; i < times; i++)
{
var q = new TermQuery(new Term("title", "volume"));
foreach (var scoreDoc in reader.Search(q, 100).ScoreDocs)
{
docs.Add(reader.Doc(scoreDoc.Doc));
}
}
}

我遇到了各种异常,例如:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative 
and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Lucene.Net.Index.FieldInfos.FieldInfo(Int32 fieldNumber)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\FieldInfos.cs:line 378
at Lucene.Net.Index.FieldsReader.Doc(Int32 n, FieldSelector fieldSelector)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\FieldsReader.cs:line 234
at Lucene.Net.Index.SegmentReader.Document(Int32 n, FieldSelector fieldSelector)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\SegmentReader.cs:line 1193
at Lucene.Net.Index.DirectoryReader.Document(Int32 n, FieldSelector fieldSelector)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\DirectoryReader.cs:line 686
at Lucene.Net.Index.IndexReader.Document(Int32 n)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\IndexReader.cs:line 732
at Lucene.Net.Search.IndexSearcher.Doc(Int32 i)
in d:\Lucene.Net\FullRepo\trunk\src\core\Search\IndexSearcher.cs:line 162
at PerformanceTest.Program.Search(IndexSearcher reader, Int32 times)
in c:\Users\Tams\Documents\Visual Studio 2012\Projects\BookCatalog\PerformanceTest\Program.cs:line 28
at PerformanceTest.Program.<>c__DisplayClass2.<Main>b__0()
in c:\Users\Tams\Documents\Visual Studio 2012\Projects\BookCatalog\PerformanceTest\Program.cs:line 43
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()

或者

System.IO.IOException: read past EOF
at Lucene.Net.Store.BufferedIndexInput.Refill()
in d:\Lucene.Net\FullRepo\trunk\src\core\Store\BufferedIndexInput.cs:line 179
at Lucene.Net.Store.BufferedIndexInput.ReadByte()
in d:\Lucene.Net\FullRepo\trunk\src\core\Store\BufferedIndexInput.cs:line 41
at Lucene.Net.Store.IndexInput.ReadVInt()
in d:\Lucene.Net\FullRepo\trunk\src\core\Store\IndexInput.cs:line 88
at Lucene.Net.Index.FieldsReader.Doc(Int32 n, FieldSelector fieldSelector)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\FieldsReader.cs:line 230
at Lucene.Net.Index.SegmentReader.Document(Int32 n, FieldSelector fieldSelector)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\SegmentReader.cs:line 1193
at Lucene.Net.Index.DirectoryReader.Document(Int32 n, FieldSelector fieldSelector)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\DirectoryReader.cs:line 686
at Lucene.Net.Index.IndexReader.Document(Int32 n)
in d:\Lucene.Net\FullRepo\trunk\src\core\Index\IndexReader.cs:line 732
at Lucene.Net.Search.IndexSearcher.Doc(Int32 i)
in d:\Lucene.Net\FullRepo\trunk\src\core\Search\IndexSearcher.cs:line 162
at PerformanceTest.Program.Search(IndexSearcher reader, Int32 times)
in c:\Users\Tams\Documents\Visual Studio 2012\Projects\BookCatalog\PerformanceTest\Program.cs:line 28
at PerformanceTest.Program.<>c__DisplayClass2.<Main>b__0()
in c:\Users\Tams\Documents\Visual Studio 2012\Projects\BookCatalog\PerformanceTest\Program.cs:line 43
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()

最后的代码工作正常,将 concurrentTaskCount 变量设置为 1。

我错过了什么吗?我不知道那是什么。

其实我没有路径

d:\Lucene.Net\FullRepo\trunk\src\core\Store\BufferedIndexInput.cs

我什至没有字母“d”的驱动器

最佳答案

source for MMapDirectory显示此类不使用 memory-mapped files , 正如预期的那样。它使用 MemoryStream 对象将所有索引文件加载到内存中,我猜想这些流是不同线程查找和读取时出现问题的原因。

您可以通过将其加载到 RAMDirectory 来获得基于内存的索引。这通过了你的测试。 (但它做的是 MMapDirectory 目前做的,不一定是你期望它做的......)

var fsDirectory = FSDirectory.Open(directoryInfo);
var directory = new RAMDirectory(fsDirectory);

关于c# - 有趣的 Lucene.net 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16312063/

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