使用特殊字符在 solr 中搜索

标签 search solr lucene full-text-search 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"/>

最佳答案

您正在使用标准 text_general title 属性的字段。这可能不是一个好的选择。 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执行以下操作:

A good general purpose tokenizer that strips many extraneous characters and sets token types to meaningful values. Token types are only useful for subsequent token filters that are type-aware of the same token types.


这意味着“-”字符将被完全忽略并用于标记字符串。

"kong-fu" will be represented as "kong" and "fu". The '-' disappears.


这也解释了为什么 select?q=title:\-不会在这里工作。
选择更合适的字段类型:
而不是 StandardTokenizerFactory您可以使用 solr.WhitespaceTokenizerFactory , 仅在空格上拆分以精确匹配单词。因此,为 title 属性创建自己的字段类型将是一个解决方案。
Solr 还有一个名为 text_ws 的字段类型.根据您的要求,这可能就足够了。

关于使用特殊字符在 solr 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18277609/

相关文章:

java - 使用 JavaConfig 的 Spring Data Solr 存储库

elasticsearch - 为什么语音搜索比普通匹配查询慢得多

search - 高效返回 Lucene 中所有查询命中的字段

php - 当我在 php 中尝试 Solr 时出现问题

tomcat - Solr 错误加载类 solr.SpatialRecursivePrefixTreeFieldType

search - 使用字段值影响 Solr 搜索结果

algorithm - 此数组中的最佳搜索条件?

search - 每天索引中的文档

ruby-on-rails - 轮胎 : Eager loading associations from multiple models

python - 在字符串中查找子字符串,但仅当整个单词?