(九)solr实现autocomplete功能

autocomplete 是什么相信你懂的,真的不知道的话,那就问百度吧

直接上配置

schema.xml

 <field name="id"     type="string"   indexed="true"  stored="true"  required="true"/>
 <field name="title" type="text" indexed="true" stored="true"/>

类型定义:

   <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
   <fieldtype name="text" class="solr.TextField">
    <analyzer>
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>

solrconfig.xml

<searchComponent class="solr.SpellCheckComponent" name="suggester">
    <lst name="spellchecker">
    <str name="name">suggester</str>
    <str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
    <str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str>
    <str name="field">title</str>
    <float name="threshold">0.01</float>
    <str name="buildOnCommit">true</str>
    </lst>
</searchComponent>

<requestHandler class="org.apache.solr.handler.component.SearchHandler" name="/suggester">
    <lst name="defaults">
    <str name="spellcheck">true</str>
    <str name="spellcheck.dictionary">suggester</str>
    <str name="spellcheck.count">10</str>
    </lst>
    <arr name="components">
    <str>suggester</str>
    </arr>
</requestHandler>

通过下面的方式,测试结果:

http://localhost:8983/solr/suggester/?q=%E8%BF%99%E6%98%AF

<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">0</int>
</lst>
<
lst name="spellcheck"> <lst name="suggestions"> <lst name="这是"> <int name="numFound">2</int> <int name="startOffset">0</int><int name="endOffset">2</int> <arr name="suggestion"> <str>这是什么标题呢</str> <str>这是测试文章</str> </arr> </lst> </lst> </lst> </response>

更多信息可以参考:

http://wiki.apache.org/solr/Suggester?highlight=%28autocomplete%29

posted @ 2012-10-29 12:12  涛光  阅读(1056)  评论(0编辑  收藏  举报