Lucene.NET Did you mean? - easy how to…

Sunday, May 10th, 2009

This is actually very simple to do and you can get it done in about 20 mins! I did a bit of Googling and could not find much about it so I thought that I should share my findings.

First of all you need an additional assembly called SpellChecker.Net. This assembly is a part of lucene.net and can be found in the contrib directory. I checked it out from SVN here: https://svn.apache.org/repos/asf/incubator/lucene.net/trunk/C#/contrib/SpellChecker.Net

Just open the solution and then build it in release configuration. That’s it - you now have the SpellChecker dll you need.

Now all you have to do is add a reference to this in the project that you have lucene etc (you get what I am on about I am sure).

Ok so now for the fun bit - actually using it! This is simpler than you might expect. In my case I added it to my SearchEngine class. This may be the right or wrong thing to do but for now it felt right - I perform a search on the engine - why not ask it for the suggestions too?

Ok so here is the code:
private void createSuggestionList()
{
var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(_searcher.Reader.Directory());
spellChecker.IndexDictionary(new LuceneDictionary(_searcher.Reader, "title"));
_suggestions = spellChecker.SuggestSimilar(_originalSearchTerm, 5);
}

Ok so let me explain:

  • Firstly we create an instance of the spell checker and pass in a directory. ‘_searcher’ is an instance of ‘IndexSearcher’ (you should be aware of one of these are if you are using Lucene)
  • Then we tell the spellChecker which index to read and which field to gather its spelling suggestions from
  • All we have to do now is grab an array of strings from a search term

I told you it was easy - enjoy….

lucene.net and using luke

Thursday, April 16th, 2009

I am currently tweaking one of my sites that have a lucene search. After upgrading to a newer version I now have a static method on QueryParser to escape all of those reserved characters: QueryParser.Escape(”query string”) - great I can get rid of my naff regular expression that look ugly to do this! (http://www.javalobby.org/java/forums/t86124.html)

Also just so you do not forget - when using Luke and trying to build queries do not forget to change the search analyser - when searching for terms that have numbers in them. The default ‘SimpleAnalyzer’ does not work, change it to the ‘StandardAnalyzer’ - yippy now it works (a give away was looking in the parsed window - no numbers!)

Now on with adding a spell checker!

Creating Lucene search queries

Sunday, August 19th, 2007

Useful referance:

http://lucene.apache.org/java/docs/queryparsersyntax.html

Luke for Lucene Indexes

Tuesday, July 3rd, 2007

Ever need to have a look at your Lucene indexes?

This is a nice java app that lets you play with your indexes:

http://www.getopt.org/luke/