Lucene.NET

An example of how to create a Lucene.NET index, add a document and search it:

using System;
using System.IO;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;

class Program
{
    static void Main(string[] args)
    {
        // create the index
        var directory = FSDirectory.Open(new DirectoryInfo(@"d:\index\"));
        var analyzer = new SimpleAnalyzer();
        var indexWriter = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
        var doc = new Document();

        // read a file into the index (in this case a file containing lorem ipsum)
        using (StreamReader sr = new StreamReader(@"d:\foo.txt"))
        {
            doc.Add(new Field("id", @"D:\foo.txt", Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("content", sr.ReadToEnd(), Field.Store.YES, Field.Index.ANALYZED));
        }
        indexWriter.AddDocument(doc);
        indexWriter.Optimize();
        indexWriter.Commit();
        indexWriter.Close();
        // we have created and saved the index

        // create a query against the index
        var queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "content", analyzer);
        var query = queryParser.Parse("Lorem");
        var indexSearcher = new IndexSearcher(directory, true);
        var hits = indexSearcher.Search(query);     // this method is to be deprecated, however, I can't find an easier way of doing it!

        // display the results
        Console.WriteLine("Found {0} results", hits.Length());
        for (int i = 0; i < hits.Length(); i++)
        {
            var doc2 = hits.Doc(i);
            Console.WriteLine("Result num {0}, score {1}", i + 1, hits.Score(i));
            Console.WriteLine("ID: {0}", doc2.Get("id"));
            Console.WriteLine("Text found: {0}" + Environment.NewLine, doc2.Get("content"));
        }

        // close 
        indexSearcher.Close();
        directory.Close();
    }
}

Never have I worked with a library with so many obsolete and deprecated methods. It’s quite hard going!

Advertisements