【问题标题】:Why I can not search the first word or last word in a Solr field?为什么我无法搜索 Solr 字段中的第一个单词或最后一个单词?
【发布时间】:2016-12-01 04:08:17
【问题描述】:

我是 Solr 初学者,我刚刚在我的项目中使用了 1 个月,从第一次开始,一切都很好,但我遇到了问题。如果我有这样一句话“当你爱一个人时,世界就在发光”。如果我使用“当你”或“正在发光”,则没有结果,但当我尝试使用“你爱”或“世界是”,或者只是“爱”或类似时,结果就会出现。想问一下schmal.xml文件怎么配置,还是我做错了什么,谢谢!

这里是 schema.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<schema name="minimal" version="1.1">
  <field name="_version_" type="long" indexed="true" stored="false" />
  <field name="_root_" type="string" indexed="true" stored="false" docValues="false" />

  <uniqueKey>id</uniqueKey>
  <solrQueryParser defaultOperator="AND"/>

  <field name="dplname" type="text_general" multiValued="false" indexed="true" required="true" stored="true"/>
  <field name="mail" type="text_general" indexed="true" stored="true"  multiValued="true"/>
  <field name="phone" type="text_general" indexed="true" stored="true"/>

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

  <copyField source="dplname" dest="text"/>
  <copyField source="mail" dest="text"/>
  <copyField source="phone" dest="text"/>

  <fieldType name="int" class="solr.TrieIntField" docValues="true" precisionStep="0" positionIncrementGap="0"/>
  <fieldType name="float" class="solr.TrieFloatField" docValues="true" precisionStep="0" positionIncrementGap="0"/>
  <fieldType name="long" class="solr.TrieLongField" docValues="true" precisionStep="0" positionIncrementGap="0"/>
  <fieldType name="double" class="solr.TrieDoubleField" docValues="true" precisionStep="0" positionIncrementGap="0"/>

  <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
  <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>

  <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" />
      <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
    <analyzer type="query">
      <tokenizer class="solr.StandardTokenizerFactory"/>
      <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
      <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
      <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
  </fieldType>
</schema>

更新:我使用这个查询来搜索:dplname:is shiny 或类似的。

【问题讨论】:

  • 你搜索的怎么样了?如您的查询是什么?
  • 嗨,我的查询是 dplname:*is shiny* 或类似的。
  • 您是否有意进行通配符搜索?如果你只这样搜索会发生什么dplname:shining
  • 它有效,但我想问一下我是按正在发光还是“正在发光”搜索,没有结果

标签: xml solr lucene


【解决方案1】:

好的。所以你需要了解如何在 solr 中分析和标记文本。 在你的情况下,如果你查看你的 schema.xml

<analyzer type="index">
      <tokenizer class="solr.StandardTokenizerFactory"/>
      <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
      <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>

这意味着在索引文档时将应用StandardTokenizerFactory,这会根据空格和其他一些分隔符打破句子。

阅读此处了解详情https://cwiki.apache.org/confluence/display/solr/Tokenizers#Tokenizers-StandardTokenizer)

例如你的句子:

当你爱一个人时,世界就会发光

将分解为以下标记

当,你,爱,某人,那个,世界,是,闪耀的时候

所以总共有 8 个标记。注意 , 也将被删除,因为这也是一个分隔符。

然后应用StopFilterFactory 过滤器,它将删除存在于您的 stopwords.txt 文件中的停用词。 (停用词是您不想索引的常用词,因为它们在搜索中没有意义。

阅读这里https://cwiki.apache.org/confluence/display/solr/Filter+Descriptions#FilterDescriptions-StopFilter)

假设停用词是

你,那个,是

所以在第二个过滤器之后,您会留下这些标记(因为停用词已被删除)

何时,爱,某人,世界,闪耀

现在第三个过滤器是小写过滤器,它将所有标记转换为小写。

所以总结一下什么时候说过和做过你的句子

当你爱一个人时,世界就会发光

被索引到以下标记中

何时,爱,某人,世界,闪耀

让我们谈谈搜索又名查询

在您的 schema.xml 中,您有以下内容

<analyzer type="query">
      <tokenizer class="solr.StandardTokenizerFactory"/>
      <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
      <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
      <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>

这意味着将为每个查询执行上述分析器。

所以当你搜索dplname:shining StandardTokenizerFactory 将对其进行分析,因为没有分隔符 shining 不会发生任何事情,因为它也不是停用词,也不会被 StopFilterFactory 删除,LowerCaseFilterFactory 只会将其更改为小写。(如果它已经不是)

所以 solr 将搜索的最终标记是 shining,它会在索引中找到它,因此您会得到结果。

让我们看看另一个查询

dplname:正在发光

注意:该字段仅对其直接前面的词有效,因此在上述查询中,isdplname 字段中搜索,但由于shining 前面没有任何内容,因此将在默认字段(在本例中为文本字段)。

所以基本上查询变成了(因为 defaultOperator 是 AND 它将被添加到查询中)

dplname:is AND text:shining

所以 solr 正在搜索在 dplname 字段中具有 is 并在文本字段中具有 shining 的文档。它找不到。

在此处阅读查询解析:http://lucene.apache.org/core/2_9_4/queryparsersyntax.html

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    相关资源
    最近更新 更多