Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

.net core - Can not create instance of Lucene.Net StandardAnalyzer

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.

question from:https://stackoverflow.com/questions/65641448/can-not-create-instance-of-lucene-net-standardanalyzer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 FilesdotnetpacksMicrosoft.NETCore.App.Ref5.0.0 ef et5.0System.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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...