4

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

  1. -
  2. &
  3. +

询问

  1. solr?q=Healing - Live

  2. solr?q=Healing & Live

错误信息

客户端发送的请求在语法上不正确(org.apache.lucene.queryParser.ParseException: Cannot parse '("Healing \': Lexical error at line 1, column 8. Encountered: after : "\"Healing \")。

架构.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

4 回答 4

9

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

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

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

于 2012-09-27T11:56:09.033 回答
2

基于 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);

    //现有代码
    …………………………………………………………………………

}
于 2015-09-04T18:06:10.417 回答
1

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

于 2015-02-11T13:58:15.673 回答
0

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

例如:

Healing NOT Live
Healing AND Live
Healing OR Live
于 2012-10-07T12:42:47.313 回答