I am trying to use Lucene.NET and I am following documentation provided on this link https://lucenenet.apache.org/
I am using Lucene version (4.8.0-beta00012) with .NET Core 5.0 version.
I am getting exception while creating instance of StandardAnalyzer object.
const LuceneVersion AppLuceneVersion = LuceneVersion.LUCENE_48;
// Create an analyzer to process the text
var analyzer = new StandardAnalyzer(AppLuceneVersion);
Exception that I am getting is
The type initializer for 'Lucene.Net.Analysis.Standard.StandardAnalyzer' threw an exception.
Method not found: 'System.Collections.Generic.List`1<!!0> Lucene.Net.Support.Arrays.AsList(!!0[])'.
Any help is appreciated, as I am stuck with this and I am not able to proceed with my POC.
That's a really odd error message to be getting. It appears to be saying that the system can't locate generic List type. In .Net 5 this type is stored here:
region Assembly System.Collections, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0\ref\net5.0\System.Collections.dll
While you are seeing the error because StandardAnalyzer uses this type and the system can't locate it, it's quite possible that the issue has nothing to do with Lucene.Net per se. You can test that theory by trying to do the following:
List<string> list = new List<string>();
list.Add("hello world");
There is a very good change that code will blow up the same way. If so then the issue isn't related to Lucene.
What I can say for sure is that Lucene. 4.8 Beta, is extremely feature complete, very stable and the StandardAnalyzer
works great on my system using .Net 5.0. I happen to be using Lucene.Net 4.8.0-beta00013 currently with .Net 5 but I've been using Lucene.Net with other version of .Net Core since Lucene.Net version 4.8.0-beta0006, and my personal experience is that it's an amazing search library.
To test out the Lucene.Net StandardAnalyzer on .Net 5.0 for you I created the unit test below in xUnit. The unit test passes and demonstrates that StandardAnalyzer
is working. This code could probably be simplified a bit since it uses an approach that provides a Near Real Time reader but it will give you a meaty sample to work with once you resolve why your system can't find the generic List class.
As others have pointed out in the comments, be sure to use the nuget for Lucene.Net.Analysis.Common
as its required to run this code. Also, as you are probably aware, LuceneNET 4.8 is still marked pre-release so you will need to check that checkbox in Visual Studio when looking for the nuget package or if installing the nuget from the command line you will need the -pre switch.
[Fact]
public void TestStandardAnalyzer() {
Directory indexDir = new RAMDirectory();
Analyzer standardAnalyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);
IndexWriterConfig indexConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, standardAnalyzer);
indexConfig.UseCompoundFile = true;
IndexWriter writer = new IndexWriter(indexDir, indexConfig);
//souce: https://github.com/apache/lucenenet/blob/Lucene.Net_4_8_0_beta00006/src/Lucene.Net/Search/SearcherFactory.cs
SearcherManager searcherManager = new SearcherManager(writer, applyAllDeletes: true, new SearchWarmer());
Document doc = new Document();
doc.Add(new StringField("examplePrimaryKey", "001", Field.Store.YES));
doc.Add(new TextField("exampleField", "Unique gifts are great gifts.", Field.Store.YES));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new StringField("examplePrimaryKey", "002", Field.Store.YES));
doc.Add(new TextField("exampleField", "Everyone is gifted.", Field.Store.YES));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new StringField("examplePrimaryKey", "003", Field.Store.YES));
doc.Add(new TextField("exampleField", "Gifts are meant to be shared.", Field.Store.YES));
writer.AddDocument(doc);
writer.Commit();
searcherManager.MaybeRefreshBlocking();
IndexSearcher indexSearcher = searcherManager.Acquire();
try {
QueryParser parser = new QueryParser(LuceneVersion.LUCENE_48, "exampleField", standardAnalyzer);
Query query = parser.Parse("everyone");
TopDocs topDocs = indexSearcher.Search(query, int.MaxValue);
int numMatchingDocs = topDocs.ScoreDocs.Length;
Assert.Equal(1, numMatchingDocs);
Document docRead = indexSearcher.Doc(topDocs.ScoreDocs[0].Doc);
string primaryKey = docRead.Get("examplePrimaryKey");
Assert.Equal("002", primaryKey);
} finally {
searcherManager.Release(indexSearcher);
}
}