Lucene全文搜索引擎你是说功能
如何在lucene全文搜索引擎中实现你的意思和拼写检查功能 创建索引后,您可以使用拼写检查器使用的词典创建索引,方法是:Lucene全文搜索引擎你是说功能,lucene,Lucene,如何在lucene全文搜索引擎中实现你的意思和拼写检查功能 创建索引后,您可以使用拼写检查器使用的词典创建索引,方法是: public void createSpellChekerIndex() throws CorruptIndexException, IOException { final IndexReader reader = IndexReader.open(this.indexDirectory, true); final Dictionary dic
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]
你看过Lucene的拼写检查器了吗?你的意思是说功能正在工作,但有一些问题它没有正确搜索,结果应该排在第一位,但它排在3或4位,没有给出确切的单词。你真的没有提供任何关于你的问题的信息。如果你能提供一些关于到底哪里出了问题以及你做了哪些尝试的细节,那么帮助你可能会更容易。