【问题标题】:Sorting by custom score in Solr does not sort consistently在 Solr 中按自定义分数排序的排序不一致
【发布时间】:2011-09-28 20:07:20
【问题描述】:

我为我的 Solr 数据库中的每个文档分配了一个自定义的“受欢迎程度”分数。我希望搜索结果按此自定义“分数”字段排序,而不是默认的内置相关性分数。

首先我定义我的分数字段:

<fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>
<field name="score" type="sint" stored="true" multiValued="false" />

然后我重建索引,为每个文档插入一个分数。 要运行查询,我使用如下内容:

(text:hello)+_val_:"score"

现在我希望文档返回按“分数”字段排序,但我得到的是:

<doc>
  <int name="score">566</int>
  <str name="text">SF - You lost me at hello...</str>
</doc>
<doc>
  <int name="score">41</int>
  <str name="text">hello</str>
</doc>
<doc>
  <int name="score">77</int>
  <str name="text">
    CAGE PAGE-SAY HELLO (MIKE GOLDEN's Life Is Bass Remix)-VIM
  </str>
</doc>
<doc>
  <int name="score">0</int>
  <str name="text">Hello Hello Hello</str>
</doc>

请注意,分数是乱序返回的:566、41、77、0。奇怪的是,它只对某些查询进行这种排序。我不确定模式是什么,但到目前为止,我只在搜索结果中返回“0”分数时看到排序错误。

我尝试使用 IntField 而不是 SortableIntField,并且尝试将“sort=score desc”作为查询参数,但行为没有任何变化。

我是不是做错了什么,或者只是误解了在我的查询中使用 val:"score" 的含义?

编辑:我尝试将“score”字段重命名为“popularity”并得到相同的结果。

【问题讨论】:

    标签: search sorting solr


    【解决方案1】:

    Solr 内部使用分数字段,因此定义具有相同字段名称的字段可能不是一个好习惯。
    您可以尝试使用不同的字段名称定义一个字段,并且您提到的两个选项都应该可以正常工作。

    编辑 - 这就是我所拥有的并且工作正常(Solr 3.3)

    架构 -

    字段类型-

    <fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>
    

    字段 -

    <field name="popularity" type="int" indexed="true" stored="true" />
    

    数据 -

    <add>
        <doc>
          <field name="id">1007WFP</field>
          <field name="popularity">566</field>
          <field name="text">SF - You lost me at hello...</field>
        </doc>
        <doc>
          <field name="id">2007WFP</field>
          <field name="popularity">41</field>
          <field name="text">hello</field>
        </doc>
        <doc>
          <field name="id">3007WFP</field>
          <field name="popularity">77</field>
          <field name="text">
            CAGE PAGE-SAY HELLO (MIKE GOLDEN's Life Is Bass Remix)-VIM
          </field>
        </doc>
        <doc>
          <field name="id">4007WFP</field>
          <field name="popularity">0</field>
          <field name="text">Hello Hello Hello</field>
        </doc>
    </add>
    

    查询 -

    http://localhost:8983/solr/select?q=*:*&sort=popularity%20desc
    

    结果:-

    <result name="response" numFound="4" start="0">
      <doc>
        <str name="id">1007WFP</str>
        <int name="popularity">566</int>
      </doc>
    
      <doc>
        <str name="id">3007WFP</str>
        <int name="popularity">77</int>
      </doc>
      <doc>
        <str name="id">2007WFP</str>
        <int name="popularity">41</int>
    
      </doc>
      <doc>
        <str name="id">4007WFP</str>
        <int name="popularity">0</int>
      </doc>
    </result>
    

    【讨论】:

    • 抱歉,我尝试将字段重命名为“流行度”,重新构建索引,得到了相同的结果。
    • 另外,没有看到字段被标记为 true,这将不允许对该字段进行排序并且也会引发错误。
    • 我用 indexed="true" 和 indexed="false" 都试过了,但没有效果。我没有收到 indexed="false" 的错误消息。
    • 我尝试了这个示例,并且在 Solr 3.3 中对我来说工作正常。编辑了答案你可以试试。
    • 将我的查询更改为使用“sort=popularity desc”,并将“score”重命名为“popularity”,现在它可以正确排序了。
    【解决方案2】:

    _val_ hack 实际上将“流行度”字段添加到正常计算的 solr 分数中。

    因此,如果您在文档 A 上的流行度=41,在文档 B 上的流行度=77,但文档 A 在关键字“hello”上的得分比 B 高 36 分以上,那么它们将在 B 之前按 A 排序.

    使用“排序”字段(如您所做的那样)完全覆盖按分数进行的正常排序。

    另一种方法是使用过滤器查询(参数 fq 而不是 q),过滤匹配的文档而不计算任何分数,然后使用 _val_ 定义您的评分公式。由于使用过滤器查询,所有检索到的文档的分数都为零,_val_ 不会受到影响,并且会按照您最初的预期运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多