1
votes

How to implement Did You Mean and Spellchecker feature in lucene full text search engine.

1
Have you looked into Lucene's SpellChecker?femtoRgon
SpellChecker did you mean feature working but there are some issues its not searching properly and result should come in first but it come 3 or 4 position and don,t give the exact word.user2783666
You really haven't provided any information about your problem. If you provide some details about exactly what is going wrong and what you have tried, it might be easier to help you.femtoRgon

1 Answers

3
votes

After you've created the index you can can create the index with the dictionary used by the spell checker using:

public void createSpellChekerIndex() throws CorruptIndexException,
        IOException {
    final IndexReader reader = IndexReader.open(this.indexDirectory, true);
    final Dictionary dictionary = new LuceneDictionary(reader,
            LuceneExample.FIELD);
    final SpellChecker spellChecker = new SpellChecker(this.spellDirectory);
    final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
    final IndexWriterConfig writerConfig = new IndexWriterConfig(
            Version.LUCENE_36, analyzer);
    spellChecker.indexDictionary(dictionary, writerConfig, true);
    spellChecker.close();
}

and than ask for a suggestions array with:

public String[] getSuggestions(final String queryString,
        final int numberOfSuggestions, final float accuracy) {
    try {
        final SpellChecker spellChecker = new SpellChecker(
                this.spellDirectory);
        final String[] similarWords = spellChecker.suggestSimilar(
                queryString, numberOfSuggestions, accuracy);
        return similarWords;
    } catch (final Exception e) {
        return new String[0];
    }
}

Example: After indexing the following document:

    luceneExample.index("spell checker");
    luceneExample.index("did you mean");
    luceneExample.index("hello, this is a test");
    luceneExample.index("Lucene is great");

And creating the spell index with the method above, i tried to search for the string "lucete" and, asking for suggestion with

 final String query = "lucete";
 final String[] suggestions = luceneExample.getSuggestions(query, 5,
            0.2f);
 System.out.println("Did you mean:\n" + Arrays.toString(suggestions));

This was the output:

Did you mean:
[lucene]