天天看點

Solr進階搜尋【拼寫檢查】

一.拼寫檢查常用場景

  1.查詢包括一個或多個拼寫錯誤的詞,導緻結果中得到不相關的内容。如果查詢建議可用,搜尋引擎應自動執行查詢建議,向使用者顯示一條消息,如“顯示的是xxx的搜尋結果”或“仍然搜尋xxx”。

  2.查詢包括罕見詞,沒有傳回什麼搜尋結果。與此同時,存在可用的查詢建議,并且能夠得到多一些搜尋結果。在這種情況下,搜尋引擎提示使用者“你是不是要找。。。?”。

  3.查詢包括拼寫正确的詞項。雖然存在可用的查詢建議,但兩者的搜尋結果情況差不多。在這種情況下,搜尋引擎無需向使用者提供建議。

  4.查詢包括索引中不存在的詞項,沒有可用的查詢建議。

從這4種情況可以得出拼寫檢查使用的兩個關鍵要求。首先,需要一種方法識别出查詢中每個詞項的建議詞。也就是說,在某種詞典中查找與使用者輸入查詢詞項相似的詞項。其次,需要知道每個建議詞比對了多少文檔,這會對是否給出查詢建議以及如何提示使用者起到一定參考作用。

二.拼寫檢查案例

  1.拼寫檢查

  

Solr進階搜尋【拼寫檢查】

   

Solr進階搜尋【拼寫檢查】

   2.搜尋不存在的詞項

Solr進階搜尋【拼寫檢查】

   3.忽略查詢建議

Solr進階搜尋【拼寫檢查】

 三.拼寫檢查搜尋元件

  拼寫檢查主要通過使用/spell請求處理器進行送出。預設配置如下: 

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

    <str name="queryAnalyzerFieldType">text_general</str>

    <!-- Multiple "Spell Checkers" can be declared and used by this
         component
      -->

    <!-- a spellchecker built from a field of the main index -->
    <lst name="spellchecker">
      <str name="name">default</str>
      <str name="field">text</str>
      <str name="classname">solr.DirectSolrSpellChecker</str>
      <!-- the spellcheck distance measure used, the default is the internal levenshtein -->
      <str name="distanceMeasure">internal</str>
      <!-- minimum accuracy needed to be considered a valid spellcheck suggestion -->
      <float name="accuracy">0.5</float>
      <!-- the maximum #edits we consider when enumerating terms: can be 1 or 2 -->
      <int name="maxEdits">2</int>
      <!-- the minimum shared prefix when enumerating terms -->
      <int name="minPrefix">1</int>
      <!-- maximum number of inspections per result. -->
      <int name="maxInspections">5</int>
      <!-- minimum length of a query term to be considered for correction -->
      <int name="minQueryLength">4</int>
      <!-- maximum threshold of documents a query term can appear to be considered for correction -->
      <float name="maxQueryFrequency">0.01</float>
      <!-- uncomment this to require suggestions to occur in 1% of the documents
        <float name="thresholdTokenFrequency">.01</float>
      -->
    </lst>
    
    <!-- a spellchecker that can break or combine words.  See "/spell" handler below for usage -->
    <lst name="spellchecker">
      <str name="name">wordbreak</str>
      <str name="classname">solr.WordBreakSolrSpellChecker</str>      
      <str name="field">name</str>
      <str name="combineWords">true</str>
      <str name="breakWords">true</str>
      <int name="maxChanges">10</int>
    </lst>
  </searchComponent>
        

  當需要在查詢中預設支援拼寫檢查,可以把spell裡面的配置拷貝到/select中,這樣預設查詢就會支援拼寫檢查。

  拼寫檢查參數詳解:

Solr進階搜尋【拼寫檢查】
Solr進階搜尋【拼寫檢查】

   Solr4的預設拼寫檢查元件是DirectSolrSpellChecker元件。該元件根據主索引直接提供建議。而在Solr的早期版本中,需要基于主索引建構一個單獨的拼寫檢查索引。在主索引改變後需要重新建構所有二級索引的情況,基于主索引提供建議的做法可以避免這種情況,是以該方法優于維護二級索引。

  DirectSolrSpellChecker元件可以調整多個參數,其中最重要的三個參數分别是field、distanceMeasure和accuracy。field參數指定索引中用于提供建議的字段。distanceMeasure參數告訴Solr如何确定查詢詞的建議。accuracy參數是一個介于0~1之間的浮點數,它确定了查詢建議需要的準确程度。數字越大,準确性越高,但會導緻比對結果減少,極端情況下可能沒有可用的查詢建議。如果accuracy設定值太低,Solr會産生很多查詢建議,但無法保證這些查詢建議對使用者而言是有意義的。

  用于拼寫檢查的文本分析

  考慮一下在拼寫檢查字典的詞項上應執行的文本分析類型。在大多數情況下,我們都希望文本分析粒度盡可能小,尤其要避免過濾器對詞項進行大幅度修改,例如,詞幹提取或語音分析。

  其它拼寫檢查實作方式

  Solr拼寫檢查的強大特征之一是可以将多個拼寫檢查實作方法結合起來建立複合拼寫檢查方法。

繼續閱讀