【问题标题】:How do I implement a solr spell checker?如何实现 solr 拼写检查器?
【发布时间】:2011-06-20 03:32:34
【问题描述】:

我想使用 solr 在我的搜索应用程序中实现一个拼写检查器组件。需要更改什么配置?

【问题讨论】:

    标签: lucene solr spell-checking


    【解决方案1】:

    将以下部分添加到您的 solrconfig.xml

        <searchComponent name="spellcheck" class="solr.SpellCheckComponent">
    
        <lst name="spellchecker">
          <!--
               Optional, it is required when more than one spellchecker is configured.
               Select non-default name with spellcheck.dictionary in request handler.
          -->
          <str name="name">default</str>
          <!-- The classname is optional, defaults to IndexBasedSpellChecker -->
          <str name="classname">solr.IndexBasedSpellChecker</str>
          <!--
                   Load tokens from the following field for spell checking,
                   analyzer for the field's type as defined in schema.xml are used
          -->
          <str name="field">spell</str>
          <!-- Optional, by default use in-memory index (RAMDirectory) -->
          <str name="spellcheckIndexDir">./spellchecker</str>
          <!-- Set the accuracy (float) to be used for the suggestions. Default is 0.5 -->
          <str name="accuracy">0.7</str>
          <!-- Require terms to occur in 1/100th of 1% of documents in order to be included in the dictionary -->
          <float name="thresholdTokenFrequency">.0001</float>
        </lst>
        <!-- Example of using different distance measure -->
        <lst name="spellchecker">
          <str name="name">jarowinkler</str>
          <str name="field">lowerfilt</str>
          <!-- Use a different Distance Measure -->
          <str name="distanceMeasure">org.apache.lucene.search.spell.JaroWinklerDistance</str>
          <str name="spellcheckIndexDir">./spellchecker</str>
    
        </lst>
    
        <!-- This field type's analyzer is used by the QueryConverter to tokenize the value for "q" parameter -->
        <str name="queryAnalyzerFieldType">textSpell</str>
    </searchComponent>
    <!--
      The SpellingQueryConverter to convert raw (CommonParams.Q) queries into tokens.  Uses a simple regular expression
      to strip off field markup, boosts, ranges, etc. but it is not guaranteed to match an exact parse from the query parser.
    
      Optional, defaults to solr.SpellingQueryConverter
    -->
    <queryConverter name="queryConverter" class="solr.SpellingQueryConverter"/>
    
    <!--  Add to a RequestHandler
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    NOTE:  YOU LIKELY DO NOT WANT A SEPARATE REQUEST HANDLER FOR THIS COMPONENT.  THIS IS DONE HERE SOLELY FOR
    THE SIMPLICITY OF THE EXAMPLE.  YOU WILL LIKELY WANT TO BIND THE COMPONENT TO THE /select STANDARD REQUEST HANDLER.
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    -->
    
    
    <requestHandler name="/spellCheckCompRH" class="solr.SearchHandler">
        <lst name="defaults">
          <!-- Optional, must match spell checker's name as defined above, defaults to "default" -->
          <str name="spellcheck.dictionary">default</str>
          <!-- omp = Only More Popular -->
          <str name="spellcheck.onlyMorePopular">false</str>
          <!-- exr = Extended Results -->
          <str name="spellcheck.extendedResults">false</str>
          <!--  The number of suggestions to return -->
          <str name="spellcheck.count">1</str>
        </lst>
    <!--  Add to a RequestHandler
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    REPEAT NOTE:  YOU LIKELY DO NOT WANT A SEPARATE REQUEST HANDLER FOR THIS COMPONENT.  THIS IS DONE HERE SOLELY FOR
    THE SIMPLICITY OF THE EXAMPLE.  YOU WILL LIKELY WANT TO BIND THE COMPONENT TO THE /select STANDARD REQUEST HANDLER.
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    -->
        <arr name="last-components">
          <str>spellcheck</str>
        </arr>
      </requestHandler>
    

    这个来自 Solr Wiki 的配置示例, 添加后,您可以请求构建拼写检查索引

    http://localhost:8983/solr/spell?q=some query&spellcheck=true&spellcheck.collate=true&spellcheck.build=true 
    

    请注意不要在每个请求中包含查询的最后一部分,因为这将在您请求时构建拼写索引,因此 前一个成为第一个请求之后

    http://localhost:8983/solr/spell?q=some query&spellcheck=true&spellcheck.collate=true
    

    在之前的 XML sextion 中,不要忘记将字段拼写替换为您要针对其构建拼写检查器的字段

    现在你可以感受到拼写检查的力量了

    【讨论】:

    • 这样我得到了拼写检查字典。但我不知道如何将它包含在我的搜索结果中。我的意思是在 jsp 页面中显示 serach 结果,我正在解析一个 json 对象。包括 splellchecker 我的网址应该是什么
    • 确保您已将: spellcheck 添加到您的默认查询结果处理程序中,以便运行拼写检查程序选择时。
    • @lindstromhenrik:我的请求处理程序中有 spellcheck。但我没有得到 请告诉我为什么会这样??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2011-03-01
    相关资源
    最近更新 更多