Lucene.Net is a high performance search engine library for .NET

Install-Package Lucene.Net -Pre

Create an index and define a text analyzer

// Ensures index backwards compatibility
var AppLuceneVersion = LuceneVersion.LUCENE_48;

var indexLocation = @"C:\Index";
var dir = FSDirectory.Open(indexLocation);

//create an analyzer to process the text
var analyzer = new StandardAnalyzer(AppLuceneVersion);

//create an index writer
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
var writer = new IndexWriter(dir, indexConfig);

Add to the index

var source = new
{
    Name = "Kermit the Frog",
    FavouritePhrase = "The quick brown fox jumps over the lazy dog"
};
var doc = new Document();
// StringField indexes but doesn't tokenise
doc.Add(new StringField("name", source.Name, Field.Store.YES));

doc.Add(new TextField("favouritePhrase", source.FavouritePhrase, Field.Store.YES));

writer.AddDocument(doc);
writer.Flush(triggerMerge: false, applyAllDeletes: false);

Construct a query

// search with a phrase
var phrase = new MultiPhraseQuery();
phrase.Add(new Term("favouritePhrase", "brown"));
phrase.Add(new Term("favouritePhrase", "fox"));

Fetch the results

// re-use the writer to get real-time updates
var searcher = new IndexSearcher(writer.GetReader(applyAllDeletes: true));
var hits = searcher.Search(phrase, 20 /* top 20 */).ScoreDocs;
foreach (var hit in hits)
{
    var foundDoc = searcher.Doc(hit.Doc);
    hit.Score.Dump("Score");
    foundDoc.Get("name").Dump("Name");
    foundDoc.Get("favouritePhrase").Dump("Favourite Phrase");
}

About the project

Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users

Our Goals

  • Maintain the existing line-by-line port from Java to C#, fully automating and commoditizing the process such that the project can easily synchronize with the Java Lucene release schedule
  • Maintaining the high-performance requirements expected of a first class C# search engine library
  • Maximize usability and power when used within the .NET runtime. To that end, it will present a highly idiomatic, carefully tailored API that takes advantage of many of the special features of the .NET runtime