【问题标题】:Lucene Full Text search Engine Did you mean featureLucene 全文搜索引擎 你是说功能吗
【发布时间】:2013-09-28 03:54:47
【问题描述】:

如何在 lucene 全文搜索引擎中实现 Did You Mean 和 Spellchecker 功能。

【问题讨论】:

  • 你看过 Lucene 的SpellChecker吗?
  • SpellChecker 你的意思是功能工作但有一些问题它没有正确搜索并且结果应该首先出现但它出现在 3 或 4 位置并且不要给出确切的单词。
  • 您确实没有提供有关您的问题的任何信息。如果您提供一些关于究竟出了什么问题以及您尝试过什么的详细信息,可能会更容易为您提供帮助。

标签: lucene


【解决方案1】:

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

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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 2010-11-26
    • 2011-02-28
    • 2011-06-06
    • 1970-01-01
    • 2010-11-16
    • 2011-05-16
    相关资源
    最近更新 更多