http://blog.chenlb.com/topics/450.html
spellcheck 可以實現輸入“錯誤”提示,如:輸入“周杰侖”搜索時,提示:您是不是要找“周杰倫”。
Lucene 有這樣的功能。用 n-gram 方法和 Levenshtein distance (編輯距離,算相似度)算法實現。
原理大概:用正確的詞條,以 n-gram 方法去折分,並建立索引(官方叫:dictionary index),在查詢時,去查dictionary index,可以返回正確詞條,於是這些詞條就是“提示”。
solr 1.2 也有此功能。solr 1.3 用組件的方式實現此功能。我現用 solr 1.3 來試下。默認有spellcheck組件了(使已經配置好),這次我用文件源的方式,在solr.home/conf/spellings.txt文件後加:周杰倫。注意一行一個詞。
打開上面的連接後,返回的內容,可以下面看到,如下類似的內容:
- <lst name="spellcheck">
- <lst name="suggestions">
- <lst name="周杰侖">
- <int name="numFound">1</int>
- <int name="startOffset">0</int>
- <int name="endOffset">3</int>
- <arr name="suggestion">
- <str>周杰倫</str>
- </arr>
- </lst>
- </lst>
- </lst>
用文件源的方式,一個問題就是要自己去維護“正確的詞條”,solr 還有其它方式:在索引中抽取比較高頻的詞條,這方面的應用,下次再總結與blog。