首页 文章

特殊字符( - &等)在SOLR查询中不起作用

提问于
浏览
3

我正在使用“text_general”fieldType在SOLR中进行搜索 . 在搜索特殊字符时,我没有得到正确的结果并且出错 . 我想使用这样的特殊字符:

  • -

  • &

  • +

查询

  • solr?q=Healing - Live

  • solr?q=Healing & Live

错误消息

客户端发送的请求在语法上是不正确的(org.apache.lucene.queryParser.ParseException:无法解析'(“Healing ':第1行第8列的词汇错误 . 遇到:之后:”\“Healing \”) .

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"/>

4 回答

  • -1

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

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

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

  • 1

    在基于日光浴场的Solr搜索:

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

    function suggestAction()
    {
    
        //get comp from http://[MAGE-ROOT]/solarium/ajax/suggest/?q=comp
        $comp = $this->getRequest()->getParam('q',false);
    
        //remove special characters
        $special_characters = array('(',')','/','\','&','!','.','-','+');
        $comp = str_replace($special_characters,'',$comp);
    
        //save q param
        $this->getRequest()->setParam('q',$comp);
    
        //existing code
        ...............
    
    }
    
  • 0

    StandardTokenizerFactory是您应该使用WhitespaceTokenizerFactory的问题 . 这对我有用 .

  • 8

    为什么不使用 AND OR NOT 而不是那些特殊字符 .

    例如:

    Healing NOT Live
    Healing AND Live
    Healing OR Live
    

相关问题