【问题标题】:Thinking Sphinx search through constant value通过常量值思考 Sphinx 搜索
【发布时间】:2012-12-17 19:16:28
【问题描述】:

我正在使用 rails 3.2 和 thinking_sphinx gem,并且在我的模型中有以下代码:

#encoding: utf-8
class Feedback < ActiveRecord::Base

  attr_accessible :feedback_type

  FEEDBACK_TYPE = { 1 => "found cheaper", 2 => "complaints", 3 => "wishes",
                   4 => "other" }

  define_index do
    indexes feedback_type
    has created_at, updated_at
    set_property delta: true
  end
end

在我的 feedback_controller 我有:

  @feedbacks = Feedback.search(params[:search], page: params[:page], 
                       per_page: 10,
                       sort_mode: :extended)

feedback_type 是数据库中的整数类型字段,根据 FEEDBACK_TYPE 值,它的值从 1 到 4。在视图中,我使用 Feedback::FEEDBACK_TYPE[feedback.feedback_type] 显示反馈类型。我可以通过在搜索表单中键入例如“愿望”的 FEEDBACK_TYPE 字符串值在我的模型中实现搜索吗?感谢您的帮助。

【问题讨论】:

    标签: search ruby-on-rails-3.2 thinking-sphinx


    【解决方案1】:

    实际上并没有尝试过,但以下应该可以工作:

        define_index do
            indexes "case feedback_table_name.feedback_type 
                     when 1 then 'found cheaper'
                     when 2 then 'complaints'
                     when 3 then 'wishes'
                     when 4 then 'other'
                     end",  :as=>:feedback_type
            has created_at, updated_at
            set_property delta: true
        end
    

    【讨论】:

      【解决方案2】:

      你也可以写得更强大,像这样:

      FEEDBACK_TYPE = { 1 => "found cheaper", 2 => "complaints", 3 => "wishes",
                     4 => "other" }
      
      kind_of_types = lambda do 
        str = 'case feedback_table_name. feedback_type '
        FEEDBACK_TYPE.each { |type| str += "when #{type[0]} then '#{type[1]}' " }
        str += 'end'    
      end
      
      define_index do
        indexes kind_of_types, as: :feedback_type
        has created_at, updated_at
        set_property delta: true
      end
      

      【讨论】:

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