1

I have a site that give data to the user. I want to use Lucene.Net for my autocomplete. The thing is I want to be able to return results that correct spelling errors. I see that Lucene.Net has a spellchecker functionality that suggest other words. But it returns the words and I need the Ids in order to get more info of that item. Do I have to do another query on the regular index after I get results from the spellchecker or is there a better way???

Gidi
  • 181
  • 1
  • 10

1 Answers1

4

You will need to search for it, it cannot do it since spellchecking works on a separate index that is not linked to you main index your created suggestions from.

Its easy to do tho:

RAMDirectory dir = new RAMDirectory();
IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);

Document d = new Document();
Field textField = new Field("text", "", Field.Store.YES, Field.Index.ANALYZED);
d.Add(textField);
Field idField = new Field("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED);
d.Add(idField);

textField.SetValue("this is a document with a some words");
idField.SetValue("42");
iw.AddDocument(d);

iw.Commit();
IndexReader reader = iw.GetReader();

SpellChecker.Net.Search.Spell.SpellChecker speller = new SpellChecker.Net.Search.Spell.SpellChecker(new RAMDirectory());
speller.IndexDictionary(new LuceneDictionary(reader, "text"));
string [] suggestions = speller.SuggestSimilar("dcument", 5);


IndexSearcher searcher = new IndexSearcher(reader);
foreach (string suggestion in suggestions)
{
    TopDocs docs = searcher.Search(new TermQuery(new Term("text", suggestion)), null, Int32.MaxValue);
    foreach (var doc in docs.ScoreDocs)
    {
        Console.WriteLine(searcher.Doc(doc.Doc).Get("id"));
    }
}

reader.Dispose();
iw.Dispose();
Jf Beaulac
  • 5,206
  • 1
  • 25
  • 46
  • your answer look interesting and easy to implement. when trying that I get a few errors like: "The type 'Lucene.Net.Store.Directory' is defined in an assembly that is not referenced. You must add a reference to assembly 'Lucene.Net, Version=2.0.0.4, Culture=neutral, PublicKeyToken=null'". and "cannot convert from 'Lucene.Net.Store.RAMDirectory' to Lucene.Net.Store.Directory'". I am referencing lucene.net version 3.0.3.0. any ideas? – Gidi May 19 '13 at 07:22
  • seems like you are referencing conflicting assemblies, try removing all references and get Lucene.Net 3.0.3 and Lucene.Net 3.0.3 Contrib and do a clean rebuild. I suggest you to get the assemblies from Nuget. I built the example with 3.0.3 from nuget – Jf Beaulac May 19 '13 at 20:58
  • ok you right I fixed that. I don't understand why you need the "RAMDirectory dir = new RAMDirectory();" I don't see you use it. does it have to point to the index directory? I tried it like this and it returned empty results. – Gidi May 20 '13 at 09:50
  • i used a ram directory to build a quick self contained example, in your app you should use FSDirectories – Jf Beaulac May 20 '13 at 16:21