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",
    FavoritePhrase = "The quick brown fox jumps over the lazy dog"
Document doc = new Document
    // StringField indexes but doesn't tokenize
    new StringField("name", 
    new TextField("favoritePhrase", 

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

Construct a query

// search with a phrase
var phrase = new MultiPhraseQuery
    new Term("favoritePhrase", "brown");
    new Term("favoritePhrase", "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);
    foundDoc.Get("favoritePhrase").Dump("Favorite 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

Popular Books