How to get started with Lucene.net
In this third post I’m going to put in practice all the concepts explained the previous post, writing a simple console application that indexes the text entered in the console.
I’ll refer to the steps I outlined in my previous post. So if you haven’t already I recommend you go back and read it.
Step 1 – Initialize the Directory and the IndexWriter
As I said in my previous post, there are two possible Directory you can use: one based on the file system and one based on RAM. You’d usually want to use the FS based one: it’s pretty fast anyway, and you don’t need to constantly dump it to the filesystem. Probably the RAM is more a test fake than something to use for real in production.And once you have instantiated the Directory you have to open an IndexWriter on it.
Directory directory = FSDirectory.GetDirectory("LuceneIndex"); Analyzer analyzer = new StandardAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer);
IndexWriter writer = new IndexWriter("LuceneIndex", analyzer);
Step 2 – Add Documents to the index
I’ll cover this topic more in depth in a subsequent post, but the basic code for adding a document to the index is pretty straightforward. Create a document, add some fields to it, and then add the document to the Index.Document doc = new Document(); doc.Add(new Field("id", i.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("postBody", text, Field.Store.YES, Field.Index.TOKENIZED)); writer.AddDocument(doc);
writer.Optimize();
//Close the writer
writer.Flush();
writer.Close();
Step 3 – Create the Query
The Query can be either created via API or parsing Lucene query syntax with the QueryParser.QueryParser parser = new QueryParser("postBody", analyzer); Query query = parser.Parse("text");
Query query = new TermQuery(new Term("postBody", "text"));
Step 4 – Pass the Query to the IndexSearcher
Once you have your Query, all you need is passing it to the Search method of the IndexSearcher.//Setup searcher IndexSearcher searcher = new IndexSearcher(directory); //Do the search Hits hits = searcher.Search(query);
Step 5 – Iterates over the Results
The Search method returns a Hits object, which contains all the documents returned by the query. To list the results, just loop through all the results.int results = hits.Length(); Console.WriteLine("Found {0} results", results); for (int i = 0; i < results; i++) { Document doc = hits.Doc(i); float score = hits.Score(i); Console.WriteLine("Result num {0}, score {1}", i+1,score); Console.WriteLine("ID: {0}", doc.Get("id")); Console.WriteLine("Text found: {0}" + Environment.NewLine, doc.Get("postBody")); }
Step 6 – Close everything
Once you are done with everything, you need to close all the resources: Directory and IndexSearcher.searcher.Close(); directory.Close();
Get the code
You can download a short sample application that stitch together all that code into a console application that lets you index any text you enter, and later search for it.http://codeclimber.googlecode.com/svn/BlogPosts/Lucene.net/Basic-Concepts/Program.cs
Comments
Post a Comment