lucenet.net 版本是 4.8.0-beta00016,使用的是 StandardAnalyzer,在 GetTokenStream 时报错
System.InvalidOperationException : TokenStream contract violation: Close() call missing
Stack Trace:
at Lucene.Net.Analysis.Tokenizer.SetReader(TextReader input)
at Lucene.Net.Analysis.TokenStreamComponents.SetReader(TextReader reader)
at Lucene.Net.Analysis.Standard.StandardAnalyzer.TokenStreamComponentsAnonymousClass.SetReader(TextReader reader)
at Lucene.Net.Analysis.Analyzer.GetTokenStream(String fieldName, String text)
相关代码如下
private IEnumerable<QueryTerms> ParseFromSinglePhrase(string phrase, Analyzer queryAnalyzer)
{
using var reader = new StringReader(phrase);
var elements = queryAnalyzer
.GetTokenStream(null, reader)
.Elements()
.Select(p => p.Term)
.ToArray();
var nomalQueryTerms = new QueryTerms(QueryTermsType.Nomal, elements, new string[] { phrase });
return new[] { nomalQueryTerms };
}
请问如何解决这个问题?
多次调用 GetTokenStream
使用同一个 Analyzer
实例引起的问题,需要每次都创建 Analyzer 实例,也就是把
queryAnalyzer.GetTokenStream(null, reader)
改为
new StandardAnalyzer(LuceneVersion.LUCENE_48).GetTokenStream(null, reader)
改进后的实现
private IEnumerable<QueryTerms> ParseFromSinglePhrase(string phrase, IAnalyzerFactory analyzerFactory)
{
using var reader = new StringReader(phrase);
var elements = analyzerFactory.GetQueryAnalyzer()
.GetTokenStream(null, reader)
.Elements()
.Select(p => p.Term)
.ToArray();
var nomalQueryTerms = new QueryTerms(QueryTermsType.Nomal, elements, new string[] { phrase });
return new[] { nomalQueryTerms };
}