如何根据重量对字段(在模板中)进行排序?

时间:2011-04-25 16:29:30

标签: ruby-on-rails sorting sphinx thinking-sphinx

我使用thinking_sphinx进行搜索,我正在尝试获取输出表,其中字段根据权重进行分组(来自set_property:field_weights)

这个define_block

  define_index do
    indexes status, title, content, manager, note, start_date, end_date
    has created_at, updated_at, parent_id, organization_id

    has user_id, :as => :user_id, :type => :integer

    has '7', :as => :model_order, :type => :integer

    set_property :field_weights => {
      :title => 1,
      :start_date => 2,
      :user_id => 7
    }

    set_property :delta => true
  end

如何根据体重(在模板中)对字段进行排序?

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解Q,但您似乎正在尝试从视图中读取field_weights的配置... 我不知道think_sphinx是否有方法可以做到这一点,但我要做的是保存配置,并在需要的地方使用它...

class YourModel
    FieldWeights = {
        :title => 1,
        :start_date => 2,
        :user_id => 7
    }
  # more code here....

  define_index do
    indexes status, title, content, manager, note, start_date, end_date
    has created_at, updated_at, parent_id, organization_id

    has user_id, :as => :user_id, :type => :integer

    has '7', :as => :model_order, :type => :integer

    set_property :field_weights => YourModel::FieldWeights

    set_property :delta => true
  end
end

然后在您的视图中,您可以使用YourModel :: FieldWeights按权重顺序显示字段,例如:

YourModel::FieldWeight.invert.sort