Search code examples
c#.netlucenelucene.net

How to handle System.MissingMethodException in Lucent.net dll?


I new to Lucene & C#. I am trying to replicate the example given on Lucene.net tutorial but when i running the code it is showing

Unhandled exception. System.MissingMethodException: Method not found: 'System.IO.MemoryMappedFiles.MemoryMappedFile System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(System.IO.FileStream, System.String, Int64, System.IO.MemoryMappedFiles.MemoryMappedFileAccess, System.IO.MemoryMappedFiles.MemoryMappedFileSecurity, System.IO.HandleInheritability, Boolean)'.
   at Lucene.Net.Store.MMapDirectory.Map(MMapIndexInput input, FileStream fc, Int64 offset, Int64 length)
   at Lucene.Net.Store.MMapDirectory.MMapIndexInput..ctor(MMapDirectory outerInstance, String resourceDescription, FileStream fc)
   at Lucene.Net.Store.MMapDirectory.OpenInput(String name, IOContext context)
   at Lucene.Net.Store.Directory.Copy(Directory to, String src, String dest, IOContext context)
   at Lucene.Net.Store.TrackingDirectoryWrapper.Copy(Directory to, String src, String dest, IOContext context)
   at Lucene.Net.Index.IndexWriter.CreateCompoundFile(InfoStream infoStream, Directory directory, CheckAbort checkAbort, SegmentInfo info, IOContext context)
   at Lucene.Net.Index.DocumentsWriterPerThread.SealFlushedSegment(FlushedSegment flushedSegment)
   at Lucene.Net.Index.DocumentsWriterPerThread.Flush()
   at Lucene.Net.Index.DocumentsWriter.DoFlush(DocumentsWriterPerThread flushingDWPT)
   at Lucene.Net.Index.DocumentsWriter.FlushAllThreads(IndexWriter indexWriter)
   at Lucene.Net.Index.IndexWriter.DoFlush(Boolean applyAllDeletes)
   at Lucene.Net.Index.IndexWriter.Flush(Boolean triggerMerge, Boolean applyAllDeletes)

I don't know to handle this error or what is error. this error occurring writing the folder , sometimes reading the folder it's just so random .

my code

const LuceneVersion luceneVersion = LuceneVersion.LUCENE_48; 

//Open the Directory using a Lucene Directory class
string indexName = "example_index";
string indexPath = Path.Combine(Environment.CurrentDirectory, indexName);


FSDirectory indexDir = FSDirectory.Open(new DirectoryInfo(indexPath));

//Create an analyzer to process the text 
Analyzer standardAnalyzer = new StandardAnalyzer(luceneVersion);

//Create an index writer
IndexWriterConfig indexConfig = new IndexWriterConfig(luceneVersion, standardAnalyzer);
//indexConfig.OpenMode = OpenMode.CREATE;                             // create/overwrite index
IndexWriter writer = new IndexWriter(indexDir, indexConfig);

//Add three documents to the index
Document doc = new Document();
doc.Add(new TextField("titleTag", "The Apache Software Foundation - The world's largest open source foundation.", Field.Store.YES));
doc.Add(new StringField("domain", "www.apache.org/", Field.Store.YES));
writer.AddDocument(doc);

doc = new Document();
doc.Add(new TextField("title", "Powerful open source search library for .NET", Field.Store.YES));
doc.Add(new StringField("domain", "lucenenet.apache.org", Field.Store.YES));
writer.AddDocument(doc);

doc = new Document();
doc.Add(new TextField("title", "Unique gifts made by small businesses in North Carolina.", Field.Store.YES));
doc.Add(new StringField("domain", "www.giftoasis.com", Field.Store.YES));
writer.AddDocument(doc);

// //Flush and commit the index data to the directory
writer.Flush(true,true) ;
writer.Commit();
writer.Dispose() ;

using DirectoryReader reader = writer.GetReader(applyAllDeletes: true);
IndexSearcher searcher = new IndexSearcher(reader);

Query query = new TermQuery(new Term("domain", "lucenenet.apache.org"));
TopDocs topDocs = searcher.Search(query,1);         //indicate we want the first 2 results


int numMatchingDocs = topDocs.TotalHits;
Document resultDoc = searcher.Doc(topDocs.ScoreDocs[0].Doc);  //read back first doc from results (ie 0 offset)
string title = resultDoc.Get("title");

Console.WriteLine($"Matching results: {topDocs.TotalHits}");
Console.WriteLine($"Title of first result: {title}");

as comments suggest i am adding .csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp6.0.0</TargetFramework>
    <TargetFrameworkVersion>v6.0.0</TargetFrameworkVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Lucene.Net" Version="4.8.0.770-beta" />
    <PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0.770-beta" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
  </ItemGroup>

</Project>

Solution

  • Problem

    The problem is that you are using a really old pre-release version of Lucene.NET prior to when .NET Core support was added which uses a confusing version numbering scheme, 4.8.0.770-beta and was released only on MyGet.org. This version will not support .NET 6.0, it only supports .NET Framework.

    <ItemGroup>
      <PackageReference Include="Lucene.Net" Version="4.8.0.770-beta" />
      <PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0.770-beta" />
    </ItemGroup>
    

    Solution

    To fix this, use one of the beta versions on NuGet.org instead.

    <ItemGroup>
      <PackageReference Include="Lucene.Net" Version="4.8.0-beta00016" />
      <PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00016" />
    </ItemGroup>
    

    Also, be sure to change the target framework moniker to net6.0 instead of net6.0.0. TargetFrameworkVersion is unnecessary for .NET 6.0.

    <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>netcoreapp6.0</TargetFramework>
    </PropertyGroup>