提问者:小点点

更新到Lucene.NET 4.8.0-Beta00001后代码中断


我刚开始在一个项目中使用Lucene.NET。我的代码基于这里提供的代码:https://github.com/synhershko/lucenenetdemo by 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()); });

错误消息:

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

无法通过类型为“analyzer”的限定符访问受保护成员“analyzer.CreateComponents(string,TextReader)”;限定符必须是“HtmlStripAnalyzerWrapper”类型(或从它派生)


共1个答案

匿名用户

以下是对演示的更新:https://github.com/nightowl888/lucenenetdemo

该API被无意中删除,因为它没有被正确标记,并且在Lucene 4.8.0中不存在。但是,它只是searchermanager.acquire()searchermanager.release()的补充API。您可以在Lucene4.8.0的SearcherManager文档中看到它的用法。

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体验。请参见拉请求207中第二个选项的示例。欢迎反馈。

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()},
});