【问题标题】:Apache Solr Escape QueryApache Solr 转义查询
【发布时间】:2012-10-11 23:39:21
【问题描述】:

我正在使用带有Solarium PHP 客户端库的 Apache Solr。

问题是特殊字符,破折号 (-)。

当我的搜索查询中有破折号时,我没有得到任何匹配项。

我尝试使用Solarium_Query_Helper::escapeTerm() 来解决这个问题。但我再也没有得到任何匹配。破折号用反斜杠 \ 转义。

这个问题的解决方法是什么?

我正在考虑在索引时转义所有字段,但这听起来不是一个好主意。

这是我schema.xml的一部分:

 <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" />
    <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>
...
<fields>
...
  <field name="myfield" type="text_general" indexed="true" stored="true" />
</fields>
...
<defaultSearchField>text</defaultSearchField>
<copyField source="myfield" dest="text" />

【问题讨论】:

    标签: php solr search-engine solarium


    【解决方案1】:

    您需要转义一些特殊字符。他们列出了here

    + - && || ! ( ) { } [ ] ^ " ~ * ? : \
    

    您可以使用反斜杠 \ 转义它们。我不是日光浴室专家,但您使用的功能似乎可以完成需要完成的工作。可能还有另一个原因导致您没有得到预期的匹配结果。

    【讨论】:

    • 是的,我正在寻找这个原因和解决方案。谢谢。
    • 我想您需要提供更多信息。根据提供的信息,很难为您提供解决方案。如果您的查询被正确转义,这一切都取决于您的文档是如何被索引的。
    • 我没有做任何特别的事情。我按原样索引文档,没有转义。如果文档字段包含破折号,我按原样索引它们(例如foo-bar)。
    • 您能否发布您正在查询的字段的 schema.xml fieldType 定义?
    • 看过了。 StandardTokenizerFactory 可以做一些魔术,但您在查询和索引时都应用它,所以它不应该是一个问题。您是否尝试过查看 solr 分析页面来检查您的字段是如何被索引和查询的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多