2013-09-23 38 views
0

如何在lucene全文搜索引擎中實現Did You Mean和Spellchecker功能。Lucene全文搜索引擎你的意思是功能

+0

你有沒有看着Lucene的[ 'SpellChecker'](http://lucene.apache.org/core/4_4_0/suggest/org/apache/lucene/search/spell/SpellChecker.html)? – femtoRgon

+0

SpellChecker你的意思是功能工作,但有一些問題,它不正確搜索,結果應該先來,但它來到3或4的位置,不要給出確切的單詞。 – user2783666

+0

你真的沒有提供任何關於你的問題的信息。如果您提供一些有關到底發生了什麼問題以及您嘗試過的詳細信息,可能會更容易幫助您。 – femtoRgon

回答

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"); 

A ND創建上述的方法咒語指數,我試圖搜索字符串「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]