/LuceneNet48Demo

Apache Lucene.Net 4.8 for .NET Core Demo application

Primary LanguageC#Apache License 2.0Apache-2.0

LuceneNet48Demo

A demo of using Apache Lucene.NET 4.8 for .NET Core 2+ environment.

What's inside:

// make alias for fields: "t" -> "title"; "s" -> "summary"
_queryParser = new AliasMultiFieldQueryParser(MATCH_LUCENE_VERSION, new[] { "title", "sumary" }, _analyzer,
	new Dictionary<string, string>()
	{
		{ "t", "title" },
		{ "s", "summary" }
	});
	
// (query: title:term summary:cyborg)
result = search.Search("t:term s:cyborg");
Original text: My friends are visiting Montréal's engineering institutions
->
Tokens from analyzers: friend visit montreal engin institut
_analyzer = new MultiFieldAnalyzerWrapper(
	defaultAnalyzer: new EnhEnglishAnalyzer(MATCH_LUCENE_VERSION, true),
	new[]
	{
		(
			// analyzer for fields: "genre", "year"
			new[] { "genre", "year" },
			Analyzer.NewAnonymous(createComponents: (fieldName, reader) =>
			{
				var source = new KeywordTokenizer(reader);
				TokenStream result = new ASCIIFoldingFilter(source);
				result = new LowerCaseFilter(MATCH_LUCENE_VERSION, result);
				return new TokenStreamComponents(source, result);
			})
		)
	});
  • ForEachTermDocs - For get documents by Term, only selected fields to work with
// II. use term & selected fields
_writer.ForEachTermDocs(new Term("year", "1194"), new[] { "title" }, d =>
 {
	 var title = d.GetField("title").GetStringValue();
	 // do something ...
 });
  • using SearcherManager + MaybeRefreshBlocking(), Acquire() and Release()
  • using UpdateDocument() + new Term(keyField, ...) (based on article: Lucene .NET Update data)
  • using DeleteDocuments + MaybeRefreshBlocking() + new Term(keyField, ...) (for solving issue: C# Lucene.Net IndexWriter.DeleteDocuments not working
  • to check Analyzer work use PrintTokens() method to log all tokens per field
  • use example data MovieDatabase + int Id (based on code: r15h1/lucenedemo)

Packages: