簡體   English   中英

使用特殊字符在 solr 中搜索

[英]Search in solr with special characters

我在 solr 中搜索特殊字符時遇到問題。 我的文檔有一個字段“標題”,有時它可能像“泰坦尼克號 - 1999”(它有字符“-”)。 當我嘗試使用“-”在 solr 中搜索時,我收到 400 錯誤。 我試圖逃避這個角色,所以我嘗試了像“-”和“\\-”這樣的東西。 有了這些更改,solr 不會以錯誤響應我,但它返回 0 結果。

我如何在 solr 管理員中搜索具有該特殊字符(例如“-”或“'”之類的東西???

問候

更新在這里你可以看到我當前的 solr 方案https://gist.github.com/cpalomaresbazuca/6269375

我的搜索是“標題”字段。

摘自schema.xml:

 ...
 <!-- A general text field that has reasonable, generic
     cross-language defaults: it tokenizes with StandardTokenizer,
     removes stop words from case-insensitive "stopwords.txt"
     (empty by default), and down cases.  At query time only, it
     also applies synonyms. -->
    <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
        <analyzer type="index">
            <tokenizer class="solr.StandardTokenizerFactory"/>
            <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
            <!-- in this example, we will only use synonyms at query time
             <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
             -->
            <filter class="solr.LowerCaseFilterFactory"/>

        </analyzer>
        <analyzer type="query">
            <tokenizer class="solr.StandardTokenizerFactory"/>
            <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
            <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
            <filter class="solr.LowerCaseFilterFactory"/>

        </analyzer>
    </fieldType>
...
<field name="Title" type="text_general" indexed="true" stored="true"/>

您正在為 title 屬性使用標准text_general字段。 這可能不是一個好的選擇。 text_general用於大量文本(或至少是句子),而不是用於精確匹配名稱或標題。

這里的問題是text_general使用StandardTokenizerFactory

 <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
        <analyzer type="index">
            <tokenizer class="solr.StandardTokenizerFactory"/>
            <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
            <!-- in this example, we will only use synonyms at query time
             <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
             -->
            <filter class="solr.LowerCaseFilterFactory"/>
        
        </analyzer>
        <analyzer type="query">
            <tokenizer class="solr.StandardTokenizerFactory"/>
            <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
            <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
            <filter class="solr.LowerCaseFilterFactory"/>
            
        </analyzer>
    </fieldType>

StandardTokenizerFactory執行以下操作:

一個很好的通用標記器,可以去除許多無關的字符並將標記類型設置為有意義的值。 令牌類型僅對能夠識別相同令牌類型的后續令牌過濾器有用。

這意味着“-”字符將被完全忽略並用於標記字符串。

“kong-fu”將表示為“kong”和“fu”。 “-”消失。

這也解釋了為什么select?q=title:\\-在這里不起作用。

選擇更合適的字段類型:

您可以使用solr.WhitespaceTokenizerFactory代替StandardTokenizerFactory ,它只在空格上拆分以精確匹配單詞。 因此,為 title 屬性創建自己的字段類型將是一個解決方案。

Solr 還有一個名為text_ws的字段text_ws 根據您的要求,這可能就足夠了。

要搜索您的確切短語,請將引號括起來:

select?q=title:"Titanic - 1999" 

如果您只想搜索該特殊字符,則需要對其進行轉義:

select?q=title:\-

還要檢查: 特殊字符(-&+ 等)在 SOLR 查詢中不起作用

如果您確切知道不想使用哪些特殊字符,則可以將其添加到 regex-normalize.xml

<regex> 
  <pattern>&#x2D;</pattern> 
  <substitution>%2D</substitution> 
</regex>

這會將所有“-”替換為 %2D,因此當您搜索時,只要您搜索 %2D 而不是“-”,它就可以正常工作

我花了很多時間來完成這件事。 這是在 SolR 中查詢特殊字符需要完成的清晰分步操作。 希望它可以幫助某人。

  1. 編輯 schema.xml 文件並找到您正在使用的 solr.TextField。
  2. 在兩者下,“索引”和“查詢”分析器修改WordDelimiterFilterFactory並添加types="characters.txt"類似於:

     <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100" multiValued="true"> <analyzer type="index"> <tokenizer class="solr.WhitespaceTokenizerFactory"/> <filter catenateAll="0" catenateNumbers="0" catenateWords="0" class="solr.WordDelimiterFilterFactory" generateNumberParts="1" generateWordParts="1" splitOnCaseChange="1" types="characters.txt"/> </analyzer> <analyzer type="query"> <tokenizer class="solr.WhitespaceTokenizerFactory"/> <filter catenateAll="0" catenateNumbers="0" catenateWords="0" class="solr.WordDelimiterFilterFactory" generateNumberParts="1" generateWordParts="1" splitOnCaseChange="1" types="characters.txt"/> </analyzer> </fieldType>
  3. 確保您使用 WhitespaceTokenizerFactory 作為標記器,如上所示。

  4. 您的 characters.txt 文件可以包含以下條目 -

     \\# => ALPHA @ => ALPHA \# => ALPHA ie:- pointing to ALPHA only.
  5. 清除數據,重新索引和查詢輸入的字符。 它會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM