小試 solr 的 spellcheck

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文件後加:周杰倫。注意一行一個詞。

http://localhost:8080/solr/spellCheckCompRH?q=*:*&spellcheck.q=%E5%91%A8%E6%9D%B0%E4%BB%91&spellcheck=true&spellcheck.dictionary=file&spellcheck.build=true

打開上面的連接後,返回的內容,可以下面看到,如下類似的內容:

  1. <lst name="spellcheck">  
  2.  <lst name="suggestions">  
  3.   <lst name="周杰侖">  
  4.     <int name="numFound">1</int>  
  5.     <int name="startOffset">0</int>  
  6.     <int name="endOffset">3</int>  
  7.     <arr name="suggestion">  
  8.      <str>周杰倫</str>  
  9.     </arr>  
  10.   </lst>  
  11.  </lst>  
  12. </lst>  

用文件源的方式,一個問題就是要自己去維護“正確的詞條”,solr 還有其它方式:在索引中抽取比較高頻的詞條,這方面的應用,下次再總結與blog。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章