首页 新闻 会员 周边 捐助

Lucene.Net: GetTokenStream 时报错 "TokenStream contract violation: Close() call missing"

0
悬赏园豆:30 [已解决问题] 解决于 2023-01-24 10:16

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 };
}

请问如何解决这个问题?

dudu的主页 dudu | 高人七级 | 园豆:29568
提问于:2023-01-24 08:25
< >
分享
最佳答案
0

多次调用 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 };
}
dudu | 高人七级 |园豆:29568 | 2023-01-24 10:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册