原文地址: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. Rotating Sentences

    #include<bits/stdc++.h> #define N 110 int main(void) { char s[N][N]; int i, j, k, max; , memse ...

  2. Spring的lookup-method标签

    Spring的解析源码 public void parseLookupOverrideSubElements(Element beanEle, MethodOverrides overrides) { ...

  3. SpringBoot相关

    快速构建项目 第 1 步:将这个 Spring Boot 项目的打包方式设置为 war. <packaging>war</packaging> SpringBoot 默认有内嵌 ...

  4. MAXIMO系统 java webservice 中PDA移动应用系统开发

    MAXIMO系统 java webservice 中PDA移动应用系统开发  平时经常用的wince PDA手持设备调用c#写的webservice, 当然PDA也可以调用java webservic ...

  5. POJ1061 青蛙的约会(线性同余方程)

    线性同余方程$ ax \equiv b \pmod n$可以用扩展欧几里得算法求解. 这一题假设青蛙们跳t次后相遇,则可列方程: $$ Mt+X \equiv Nt+Y \pmod L$$ $$ (M ...

  6. Java unicode中文编码转换和反转

    参考网址http://www.oschina.net/code/snippet_142385_4297 http://canofy.iteye.com/blog/718659 在java的很多配置文件 ...

  7. ibatis 灵活的配置文件

    <select id="selectAllMmsRepairBySth" parameterClass="hashmap" resultMap=" ...

  8. 【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 题意:支持插入一个字符.修改一个字符,查询lcp.(总长度<=100000, 操作< ...

  9. Vijos 1061 迎春舞会之三人组舞(DP)

    题目链接 经典DP问题,通过问题,看出结论,然后倒序,然后注意条件. #include <cstdio> #include <cstring> #include <ios ...

  10. The constructor BASE64Encoder() is not accessible due to restriction on required

    在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: Access restriction : ...