小试 solr 的 spellcheck

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。

posted @ 2009-09-16 12:15  searchDM  阅读(708)  评论(2编辑  收藏  举报