Lucene全文搜索引擎您的意思是功能

时间:2013-09-23 09:40:41

标签: lucene

如何在lucene全文搜索引擎中实现您的意思和拼写检查功能。

1 个答案:

答案 0 :(得分:2)

创建索引后,您可以使用拼写检查器使用的字典创建索引:

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();
}

并且要求建议数组:

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];
    }
}

实施例: 索引以下文档后:

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

用上面的方法创建法术索引,我试图搜索字符串“lucete”,并询问建议

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

这是输出:

Did you mean:
[lucene]