原文地址:http://www.656463.com/article/iaquii.htm

拼写检查功能,能在搜索时提供一个较好用户体验,所以,主流的搜索引擎都有这个功能,在这之前,笔者先简单的说一下什么是拼写检查,其实很好理解,就是你输入的搜索词,可能是你输错了,也有可能在它的检索库里面根本不存在这个词,但是这时候它能给你返回,相似或相近的结果来帮助你校正。

举个例子,你在百度上输入在线电瓶,可能它的索引库里面就没有,但是它有可能返回在线电影,在线电视,在线观看等等一些词,这些,就用到拼写检查的功能了。

solr作为开源的搜索服务器,对于拼写检查,也提供了良好的支持,那么下面笔者就来讲下关于solr4.3的拼写检查的配置,在这之前先说明一点,作为拼写检查用,为了提高校正的准确率,一般对校正的词,不要进行分词,所以用string就好了,拼写检查的配置主要是在solrconfig.xml里面配置.
1、拼写组件spellCheckComponent配置

2、在SearchHandler /select里面配置

3、在SearchHandler /spell里面配置

按照上面3点来,就可以快速配置好拼写检查功能了,其实笔者上面写的4步,其实只配置2,3步就可以了,另外的第4步用默认值就可以了,在这里把它写出来,只是让大家有个认识

拼写组件SpellCheckComponent其实是核心的东西,在他的里面可以配置1至多个拼写检查器,启动的时候所有的检查器都会加载,这次笔者主要介绍的有2个拼写检查器,一个是默认的只对主索引做拼写校正,另外一个是自定义加载spellings.txt拼写检查库,带拼写校正索引库的检查器,其他的检查器各位道友想要使用的话就自己去看wiki了。

  1. <searchComponent name="spellcheck" class="solr.SpellCheckComponent">

  2. <!-- 查询分析器,如果不指定的话,默认会使用field字段类型的分词器 -->

  3. <str name="queryAnalyzerFieldType">string</str>

  4. <!-- 默认的校正器,只对主索引库管用 -->

  5. <lst name="spellchecker">

  6. <str name="name">default</str>

  7. <str name="field">my_word</str>

  8. <str name="classname">solr.DirectSolrSpellChecker</str>

  9. <str name="distanceMeasure">internal</str>

  10. <float name="accuracy">0.7</float>

  11. <int name="maxEdits">1</int>

  12. <int name="minPrefix">1</int>

  13. <int name="maxInspections">5</int>

  14. <int name="minQueryLength">4</int>

  15. <float name="maxQueryFrequency">0.01</float>

  16. </lst>

  17. <!--  读取拼写检查库的索引进行校正可以,使用默认配置,取消注释即可 -->

  18. <lst name="spellchecker">

  19. <str name="classname">solr.FileBasedSpellChecker</str>

  20. <str name="name">file</str>

  21. <str name="sourceLocation">spellings.txt</str>

  22. <str name="characterEncoding">UTF-8</str>

  23. <str name="spellcheckIndexDir">spellcheckerFile</str>

  24. </lst>

  25. </searchComponent>

对于上面的代码,虽然可以加载多个校正器,但是在拼写检查时,只能指定特定的一个进行校正,那么为什么要配置多个校正检查器呢?笔者个人感觉这个功能主要是方便在程序运行可以动态切换校正器。

在spellings.txt里面自定义的拼写检查词,注意编码的格式一定是要UTF-8无BOM的格式,这里面的词,会在solr服务启动时,自动创建spellcheckerFile文件夹并把内容加载到

F:\eclipse10tomcat\webapps\solr\solr_home\collections\collection1\data\spellcheckerFile 索引起来。

SeachHandler /select这个检索请求里面的配置很重要,拼写检查是否启用就是在这里面配置。

  1. <requestHandler name="/select" class="solr.SearchHandler">

  2. <!-- default values for query parameters can be specified, these

  3. will be overridden by parameters in the request

  4. -->

  5. <lst name="defaults">

  6. <str name="echoParams">explicit</str>

  7. <int name="rows">10</int>

  8. <str name="df">text</str>

  9. </lst>

  10. <!-- 这行代码非常重要,如果没有这行,拼写检查,是不起作用的-->

  11. <arr name="last-components">

  12. <str>spellcheck</str>

  13. </arr>

  14. </requestHandler>

SeachHandler /spell组件使用默认配置即可。

  1. <requestHandler name="/spell" class="solr.SearchHandler" startup="lazy">

  2. <lst name="defaults">

  3. <str name="df">text</str>

  4. <str name="spellcheck.dictionary">direct</str>

  5. <str name="spellcheck">on</str>

  6. <str name="spellcheck.extendedResults">true</str>

  7. <str name="spellcheck.collate">true</str>

  8. <str name="spellcheck.collateExtendedResults">true</str>

  9. </lst>

  10. <arr name="last-components">

  11. <str>spellcheck</str>

  12. </arr>

  13. </requestHandler>

至此,拼写检查功能已经配置完毕,可以用solr的UI页面进行主索引测试,也可以用spellCheckFile里面的副索引,测试,但是,一次只能使用一种校正方法

下面给出solrj代码
 public List<String>   spellcheck(String word){

  • List<String>  wordList=new ArrayList<String>();

  • SolrQuery query = new SolrQuery();

  • //query.set("q","name:ac");

  • //query.set("qt", "/spell");

  • //默认是主索引

  • query.set("q", "my_word:"+word+"");

  • query.set("qt", "/select");

  • //query.set("spellcheck.build", "true");//遇到新的检查词,会自动添加到索引里面

  • //query.set("spellcheck.dictionary", "file");//使用副索引,checkSpellFile里面的进行使用

  • query.set("spellcheck", "true");

  • query.set("spellcheck.count", Integer.MAX_VALUE);

  • //  params.set("spellcheck.build", "true");

  • try {

  • QueryResponse   rsp = server.query(query);

  • System.out.println("直接命中:"+rsp.getResults().size());

  • SolrDocumentList ss=rsp.getResults();

  • for(SolrDocument doc:ss){

  • System.out.println(doc.get("my_word"));

  • }

  • //…上面取结果的代码

  • SpellCheckResponse re=rsp.getSpellCheckResponse();

  • if (re != null) {

  • for(Suggestion s:re.getSuggestions()){

  • List<String> list=s.getAlternatives();

  • for(String spellWord:list){

  • System.out.println(spellWord);

  • // wordList.add(spellWord);

  • }

  • }

  • //        for(Collation s: spellCheckResponse.getCollatedResults()){

  • //            System.out.println(s.toString());

  • //        }

  • }

  • return wordList;

  • } catch (SolrServerException e) {

  • // TODO Auto-generated catch block

  • e.printStackTrace();

  • }

  • return null;

  • }

Solr4.3之拼写检查Spellcheck功能的更多相关文章

  1. solr入门之solr的拼写检查功能的应用级别尝试

    今天主要是收集了些拼写检查方面的资料和 尝试使用一下拼写检查的功能--=遇到了不少问题 拼写检查的四种配置眼下我仅仅算是成功了半个吧 --------------------------------- ...

  2. solr拼写检查配置

    拼写检查功能,能在搜索时,提供一个较好用户体验,所以,主流的搜索引擎都有这个功能. 那么什么是拼写检查,其实很好理解,就是你输入的搜索词,可能是你输错了,也有可能在它的检索库里面根本不存在这个词,但是 ...

  3. Emacs中的拼写检查

    无论是在Emacs中写英文日记(diary).Org mode笔记,还是撰写程序的注释和文档,拼写检查都是一项提高工作效率.保证成果品质的必不可缺的工具.拼写检查对于常见的文字处理软件(如Word.L ...

  4. solr特点四: SpellCheck(拼写检查)

    接下来,我将介绍如何向应用程序添加 “您是不是要找……”(拼写检查). 提供拼写建议 Lucene 和 Solr 很久以前就开始提供拼写检查功能了,但直到添加了 SearchComponent架构之后 ...

  5. WPF 4 单词拼写检查(SpellCheck)

    原文:WPF 4 单词拼写检查(SpellCheck)      在WPF中 Textbox 和RichTextBox 控件都内置了拼写检查属性,但该属性目前默认仅支持English.Spanish. ...

  6. Solr.NET快速入门(四)【相似查询,拼写检查】

    相似查询 此功能会返回原始查询结果中返回的每个文档的类似文档列表. 参数通过QueryOptions的MoreLikeThis属性定义. 示例:搜索"apache",为结果中的每个 ...

  7. 1.7.7 Spell Checking -拼写检查

    1. SpellCheck SpellCheck组件设计的目的是基于其他,相似,terms来提供内联查询建议.这些建议的依据可以是solr字段中的terms,外部可以创建文本文件, 或者其实lucen ...

  8. lucene拼写检查模块

    Lucene是Apache发布的开源搜索引擎开发工具包,不仅提供了核心的搜索功能,还提供了许多其他功能插件,例如:拼写检查功能模块. 搜索拼写检查模块实现类在lucene-suggest-x.xx.x ...

  9. python 拼写检查代码(怎样写一个拼写检查器)

    原文:http://norvig.com/spell-correct.html 翻译:http://blog.youxu.info/spell-correct.html 怎样写一个拼写检查器 Pete ...

随机推荐

  1. 【转】HBase 超详细介绍

    ---恢复内容开始--- http://blog.csdn.net/frankiewang008/article/details/41965543 1-HBase的安装 HBase是什么? HBase ...

  2. 【Linux/unix网络编程】之使用socket进行TCP编程

    实验一 TCP数据发送与接收 [实验目的] 1.熟练掌握套接字函数的使用方法. 2.应用套接字函数完成基本TCP通讯,实现服务器与客户端的信息交互. [实验学时] 4学时 [实验内容] 实现一个服务器 ...

  3. Nightmare

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  4. JAVA 获取jdbc.properties配置信息

    Properties myProperty = new Properties();String jdbcPath = PathKit.getWebRootPath()+File.separator+& ...

  5. TYVJ P1081 最近距离 Label:这不是分治!!!

    描述    在一块地上,有着n(1<=n<=2000) 头牛,输入n,再分别输入这n头牛的坐标(x,y) (1<=x<=100000,1<=y<=100000),如 ...

  6. BZOJ3091: 城市旅行

    Description Input Output Sample Input 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 1 4 1 4 Sample ...

  7. 基于nginx tomcat redis分布式web应用的session共享配置

    一.前言 nginx 作为目前最流行的开源反向代理HTTP Server,用于实现资源缓存.web server负载均衡等功能,由于其轻量级.高性能.高可靠等特点在互联网项目中有着非常普遍的应用,相关 ...

  8. js for循环,为什么一定要加var定义i变量

    我知道,有些人(譬如之前的我)写js的for循环时,都不习惯加上var,这当然是语法允许的.譬如下面. for(i=0;i<10;i++){//就不写成: var i=0 alert(i); } ...

  9. sizeToFit()使用心得

    sizeToFit()使用心得: 很多的初学者,包括我在内,当初在学习的时候,特别纠结什么时候用这个sizeToFit(). 下面我就来分享一下我的一些使用心得. 一.我们先来看看官方文档对sizeT ...

  10. GDB打印STL容器内容

    GDB调试不能打印stl容器内容,下载此文件,将之保存为~/.gdbinit就可以使用打印命令了. 打印list用plist命令,打印vector用pvector,依此类推. (gdb) pvecto ...