Reputation: 415
I have just started using Lucene.net for a project. I have based my code on the code provided here: https://github.com/synhershko/LuceneNetDemo by Itamar Syn-Hershko. After I updated to the newest NuGet, the code breaks in a couple of places. What do I need to change?
First problem:
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()); });
The errormessage:
'SearcherManager' does not contain a definition for 'ExecuteSearch' and no extension method 'ExecuteSearch' accepting a first argument of type 'SearcherManager' could be found (are you missing a using directive or an assembly reference?)
Second problem:
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));
}
}
The errormessage:
'HtmlStripAnalyzerWrapper.CreateComponents(string, TextReader)': cannot change access modifiers when overriding 'protected internal' inherited member 'Analyzer.CreateComponents(string, TextReader)'
And
Cannot access protected member 'Analyzer.CreateComponents(string, TextReader)' via a qualifier of type 'Analyzer'; the qualifier must be of type 'HtmlStripAnalyzerWrapper' (or derived from it)
Upvotes: 2
Views: 1017
Reputation: 56849
There is an update to the demo at: https://github.com/NightOwl888/LuceneNetDemo
The API was inadvertently removed because it was not properly marked and does not exist in Lucene 4.8.0. However, it is only a supplemental API to SearcherManager.Acquire()
and SearcherManager.Release()
. You can see its usage in the SearcherManager documentation of Lucene 4.8.0.
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!
}
We are considering whether to bring back the original ExecuteSearch()
API, or create a new one that can be used with a using
block for a more .NET-friendly experience. See an example of the second option in pull request 207. Feedback welcome.
Certainly, an API that swallows exceptions by default is less than ideal.
Accessibility of API members was also corrected to match Lucene. CharFilters were not intended to be used in conjunction with pre-built Analyzers for performance reasons. Instead, you must build up an Analyzer from pre-built tokenizers and filters.
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()},
});
Upvotes: 3