【问题标题】:Special characters (-&+, etc) not working in SOLR Query特殊字符(-&+ 等)在 SOLR 查询中不起作用
【发布时间】:2012-09-27 11:31:16
【问题描述】:

我在 SOLR 中使用“text_general”字段类型进行搜索。在使用特殊字符进行搜索时,我没有得到正确的结果并出现错误。我想使用这样的特殊字符:

  1. -
  2. &
  3. +

查询

  1. solr?q=Healing - Live

  2. solr?q=Healing & Live

错误信息

客户端发送的请求语法错误 (org.apache.lucene.queryParser.ParseException: 无法解析 '("Healing \':第 1 行第 8 列的词法错误。遇到:之后: "\"愈合\")。

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.ASCIIFoldingFilterFactory" />
    <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.ASCIIFoldingFilterFactory" />
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>


<field name="title" type="text_general" indexed="true" stored="true" />

<field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>

<defaultSearchField>text</defaultSearchField>

<copyField source="title" dest="text"/>

【问题讨论】:

    标签: solr


    【解决方案1】:

    StandardTokenizerFactory 是你应该使用 WhitespaceTokenizerFactory 的问题。这对我有用。

    【讨论】:

      【解决方案2】:

      您需要转义查询,因为破折号是 lucene 查询中的特殊字符。如果您想了解有关 lucene 查询语法的更多信息,请查看应该转义的其他字符 herehere

      您的查询将如下所示:solr?q=Healing \- Live

      我不知道您使用哪种语言编写代码,但如果您使用的是 Java,solrj 提供了ClientUtils#escapeQueryChars 方法。

      【讨论】:

      • 我在使用 C# 和 solr。它对 Healing \- Live 工作正常,但对于 Oot \& Aboot 却低于错误。消息 org.apache.lucene.queryParser.ParseException: Cannot parse 'Oot \': Lexical error at line 1, column 6. Encountered: after : "" description 客户端发送的请求在语法上不正确(org.apache .lucene.queryParser.ParseException:无法解析 'Oot \':第 1 行第 6 列出现词法错误。遇到: after : "")。
      • 我不确定您是否需要转义单个 &amp;。看起来你没有,但我可能错了。你需要逃避的是&amp;&amp;
      【解决方案3】:

      关于基于 Solarium 的 Solr 搜索:

      app\code\local\Module\Solarium\controllers\AjaxController.php

      函数建议动作() { //从 http://[MAGE-ROOT]/solarium/ajax/suggest/?q=comp 获取 comp $comp = $this->getRequest()->getParam('q',false); //去除特殊字符 $special_characters = array('(',')','/','\','&','!','.','-','+'); $comp = str_replace($special_characters,'',$comp); //保存q参数 $this->getRequest()->setParam('q',$comp); //现有代码 ………………………………………………………………………… }

      【讨论】:

        【解决方案4】:

        为什么不使用AND OR NOT 来代替那些特殊字符。

        例如:

        Healing NOT Live
        Healing AND Live
        Healing OR Live
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-05
          • 1970-01-01
          相关资源
          最近更新 更多