In a previous post (from here on after refered to as "part one") I described a console application that demonstrated the very basics of working with Lucene.NET. In this second we'll get to know Lucene.NET a little bit better by rewriting that console application so that it will:
- Persist the index to a directory on a harddrive and
- Enable us to manually input text that will be indexed and
- Perform searches from the console
Setting up the directory and analyzer
The first thing that we'll have to do is import some necessary namespaces and set up a directory for us to work with. You might recall from part one that a directory is a place where Lucene stores the data we add to it, the Documents. In part one we where not interested in storing the index to anything else than RAM so we used a a RAMDirectory. This time however we'll want to be able to add some text and still be able to perform searches on it the next time we run the application as that will be more like a real usage scenario. It will also be more convenient when we use the application for testing. So, instead of using a RAMDirectory we'll instead use a FSDirectory which stores the indexes in files in a specified directory on a harddrive.
A FSDirectory is created by invoking the static FSDirectory.GetDirectory()-method. It takes two parameters. The first parameter is for specifying the location of the directory and the second is a boolean which determines whether old data in the specified location should be read or overwritten. In our case we want to keep data from previous executions of the program so we'll set the second parameter to false if the directory already exists. If the directory doesn't already exists we must however set it to true so that directory will be created.
We'll also add a StandardAnalyzer as a member variable as it will be used by several of our methods.
The main interface
The next step is to create a basic interface that let's the user do three things:
- Add a new text to be indexed (as in part one we'll imagine that the text actually is a blog entry)
- Perform a search in the texts that have been added and
- Quit the application
Once the user quits the application we'll close the directory so we wont have any locks on the files in it.
When the user chooses to add text in the main menu the AddText()-method is invoked. In it we'll allow the user to enter some text that will be written to the index. If you've read part one it will look quite familliar, with one important exception. The IndexWriter we use here is instansiated with the create-parameter (the third parameter in the constructor) set to false if an index already exists. By doing so the IndexWriter will append to the existing index instead of overwriting it as it would have done if the parameter was set to true.
Performing searches and printing results
The final step is to add the Search()-method which will allow the user to enter one or several word to search for. The implementation is straight forward and is pretty much just a rewrite of what we did in part one.
This second part really hasn't introduced any new features except storing the index on disk. We do now however have a simple yet effective application for testing how Lucene performs it's searches. We have also discussed the important create-parameters of the FSDirectory.GetDirectory()-method and of the IndexWriter()-constructor.
The above code can be downloaded as a Visual Studio 2008 project here.
- Getting to know Lucene.Net part three - time to crawl
- Getting to know Lucene.Net
- Extending ASP.NET MVC Music Store with elasticsearch
- Building a search page for an EPiServer site using Truffler - Part 2
- ElasticSearch 101
- Building a search page for an EPiServer site using Truffler
- Truffler update – dotting the i’s and crossing the t’s
- Building a search page for an EPiServer 7 site with EPiServer Find