我刚刚开始使用Lucene.net进行项目。我的代码基于此处提供的代码:https://github.com/synhershko/LuceneNetDemo,来自Itamar Syn-Hershko。在我更新到最新的NuGet后,代码在几个地方中断。我需要改变什么?
第一个问题:
searcherManager.ExecuteSearch(searcher =>
{
var topDocs = searcher.Search(query, 10);
_totalHits = topDocs.TotalHits;
foreach (var result in topDocs.ScoreDocs)
{
var doc = searcher.Doc(result.Doc);
l.Add(new SearchResult
{
Name = doc.GetField("name")?.StringValue,
Description = doc.GetField("description")?.StringValue,
Url = doc.GetField("url")?.StringValue,
// Results are automatically sorted by relevance
Score = result.Score,
});
}
}, exception => { Console.WriteLine(exception.ToString()); });
错误消息:
'SearcherManager'不包含'ExecuteSearch'的定义,并且没有扩展方法'ExecuteSearch'接受类型'SearcherManager'的第一个参数可以找到(你是否缺少using指令或汇编引用?)
第二个问题:
public class HtmlStripAnalyzerWrapper : Analyzer
{
private readonly Analyzer _wrappedAnalyzer;
public HtmlStripAnalyzerWrapper(Analyzer wrappedAnalyzer)
{
_wrappedAnalyzer = wrappedAnalyzer;
}
public override TokenStreamComponents CreateComponents(string fieldName, TextReader reader)
{
return _wrappedAnalyzer.CreateComponents(fieldName, new HTMLStripCharFilter(reader));
}
}
错误消息:
'HtmlStripAnalyzerWrapper.CreateComponents(string,TextReader)':覆盖'protected internal'继承成员'Analyzer.CreateComponents(string,TextReader)'时无法更改访问修饰符
和
无法通过“Analyzer”类型的限定符访问受保护的成员'Analyzer.CreateComponents(string,TextReader)';限定符必须是'HtmlStripAnalyzerWrapper'类型(或从中派生出来)
答案 0 :(得分:3)
该演示的更新位于:https://github.com/NightOwl888/LuceneNetDemo
无意中删除了API,因为它没有正确标记,并且在Lucene 4.8.0中不存在。但是,它只是SearcherManager.Acquire()
和SearcherManager.Release()
的补充API。您可以在Lucene 4.8.0的SearcherManager documentation中查看其用法。
var searcher = searcherManager.Acquire();
try
{
var topDocs = searcher.Search(query, 10);
_totalHits = topDocs.TotalHits;
foreach (var result in topDocs.ScoreDocs)
{
var doc = searcher.Doc(result.Doc);
l.Add(new SearchResult
{
Name = doc.GetField("name")?.GetStringValue(),
Description = doc.GetField("description")?.GetStringValue(),
Url = doc.GetField("url")?.GetStringValue(),
// Results are automatically sorted by relevance
Score = result.Score,
});
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
searcherManager.Release(searcher);
searcher = null; // Never use searcher after this point!
}
我们正在考虑是否要恢复原始的ExecuteSearch()
API,或者创建一个可以与using
块一起使用的新API,以获得更友好的.NET体验。请参阅第二个选项in pull request 207的示例。欢迎反馈。
当然,默认情况下吞下异常的API不太理想。
还修正了API成员的可访问性以匹配Lucene。 CharFilters were not intended to be used in conjunction with pre-built Analyzers出于性能原因。相反,您必须使用预先构建的标记器和过滤器构建分析器。
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.CharFilters;
using Lucene.Net.Analysis.Core;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Util;
using System.IO;
namespace LuceneNetDemo.Analyzers
{
class HtmlStripAnalyzer : Analyzer
{
private readonly LuceneVersion matchVersion;
public HtmlStripAnalyzer(LuceneVersion matchVersion)
{
this.matchVersion = matchVersion;
}
protected override TokenStreamComponents CreateComponents(string fieldName, TextReader reader)
{
StandardTokenizer standardTokenizer = new StandardTokenizer(matchVersion, reader);
TokenStream stream = new StandardFilter(matchVersion, standardTokenizer);
stream = new LowerCaseFilter(matchVersion, stream);
stream = new StopFilter(matchVersion, stream, StopAnalyzer.ENGLISH_STOP_WORDS_SET);
return new TokenStreamComponents(standardTokenizer, stream);
}
protected override TextReader InitReader(string fieldName, TextReader reader)
{
return base.InitReader(fieldName, new HTMLStripCharFilter(reader));
}
}
}
analyzer = new PerFieldAnalyzerWrapper(new HtmlStripAnalyzer(LuceneVersion.LUCENE_48),
new Dictionary<string, Analyzer>
{
{"owner", new LowercaseKeywordAnalyzer()},
{"name", new RepositoryNamesAnalyzer()},
});