首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lucene全文搜索引擎你的意思是功能

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

Stack Overflow用户
提问于 2013-09-23 17:40:41
回答 1查看 2.2K关注 0票数 1

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

EN

回答 1

Stack Overflow用户

发布于 2013-09-24 14:49:05

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

代码语言:javascript
复制
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();
}

然后请求一个包含以下内容的建议数组:

代码语言:javascript
复制
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];
    }
}

示例:对以下文档进行索引后:

代码语言:javascript
复制
    luceneExample.index("spell checker");
    luceneExample.index("did you mean");
    luceneExample.index("hello, this is a test");
    luceneExample.index("Lucene is great");

使用上面的方法创建拼写索引,我尝试搜索字符串"lucete“,并使用以下命令请求建议

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

下面是输出:

代码语言:javascript
复制
Did you mean:
[lucene]
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18956094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档