【问题标题】:Sunspot order_by sort with nil values being last太阳黑子 order_by 排序,nil 值排在最后
【发布时间】:2012-01-16 05:03:25
【问题描述】:

我正在尝试按价格从小到大对太阳黑子搜索结果进行排序。

order_by :price, :asc

但是,有些商品的价格值为零。它们位于结果的前面。有没有一种好方法可以将它们显示在最后而不是将它们排除在外?

【问题讨论】:

    标签: solr sunspot sunspot-rails


    【解决方案1】:

    将属性“sortMissingLast=true”添加到 schema.xml 中价格字段的定义

    【讨论】:

      【解决方案2】:

      是的,您应该在 schema.xml 中的字段中添加 sortMissingLast=true,如下所示:

      <schema name="sunspot" version="1.0">
        <types>
          ...
          <!-- My custom types -->
          <fieldType name="sml_int" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>
        </types>
        <fields>
          ...
          <!-- My custom fields -->
          <dynamicField name="*_sml_int" type="sml_int" multiValued="false" indexed="true"/>
        </fields>
        ...
      </schema>
      

      然后在你的代码中你可以这样做:

      class MyModel < ActiveRecord::Base
        searchable do
          integer :price, as: :price_sml_int
        end
      end
      

      【讨论】:

        【解决方案3】:

        schema.xml 的字段的 fieldType 中使用属性 sortMissingLast="true"

        重启 solr 服务器。

        (隐式默认值为 false。)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多