狮身人面像搜索可选参数

时间:2012-12-27 12:16:50

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

我想使用可选参数搜索(Thinking Sphinx)。

在我的控制器中我有3个参数:

params[:model_id], params[:engine_id], params[:city_id]

控制器

  def search  
    @search = Car.search :conditions => {
      :model_id => params[:model_id],
      :engine_id => params[:engine_id],
      :city_id => params[:city_id]
      }
  end

模型

define_index do
    indexes model_id
    indexes city_id
    indexes area_id
    indexes engine_id
    indexes mileage
  end

当其中一个参数为nil时,我收到错误:

提取的来源(第4行):

1: Result:
2: %br
3: %br
4:   = render :partial => "search_item", :collection => @search, :as => :item

index car_core: syntax error, unexpected TOK_FIELDLIMIT near '  @engine_id  @city_id'

UPD1:

我的表格

= form_tag search_adverts_path, :method => :get, :id => 'admin_quick_filter' do
  %label
    = t('form.mileage')
  = text_field_tag :min_mileage, params[:min_mileage]
  = text_field_tag :max_mileage, params[:max_mileage]
  %br
  = select_tag :model_id, options_for_select(Model.all.collect{|c| [c.name, c.id]} , params[:model_id]), :include_blank => true
  %br
  = select_tag :engine_id, options_for_select(Engine.all.collect{|c| [c.name, c.id]} , params[:engine_id]), :include_blank => true
  %br
  = select_tag :city_id, options_for_select(City.all.collect{|c| [c.name, c.id]} , params[:city_id]), :include_blank => true
  %br
  %br
  = submit_tag t('form.search')


= render "search_result"

2 个答案:

答案 0 :(得分:1)

在搜索时,当您提供:conditions时,ThinkingSphinx会在您的查询中附加@filedname,以搜索该字段。而狮身人面像并不涉及nil values。因此,在将其传递给search()方法之前,应检查nil值。

  

请记住,Sphinx不支持SQL比较   运算符 - 它有自己的查询语言。 :condition选项必须   是一个散列,每个键都是一个字段,每个值都是一个字符串

答案 1 :(得分:0)

define_index do
  indexes name

  has mileage
end

Car.search:with => {:mileage => 100..150000}

曾为