Rails太阳黑子价格范围方面

时间:2014-06-04 05:32:39

标签: ruby-on-rails solr sunspot faceted-search sunspot-rails

我试图在我的某个型号上设置价格面,但我似乎无法让它正常工作。无论我选择哪个方面,它只会返回价格等于0美元的商品。我不确定我在这里失踪了什么。任何帮助将非常感激。这是我的代码:

移植

class CreateListings < ActiveRecord::Migration
  def change
    create_table :listings do |t|
      t.references :member
      t.text :title
      t.text :link
      t.text :category
      t.text :description
      t.decimal :price, :precision => 8, :scale => 2

      t.timestamps
    end
    add_index :listings, :member_id
    add_attachment :listings, :feature
  end
end

模型

class Listing < ActiveRecord::Base

    searchable :auto_index => true, :auto_remove => true do
      text :title, :boost => 5
      text :marker_list, :boost => 2
      text :category
      string :marker_list, :multiple => true, :stored => true
      float :price
    end

end

控制器

class ListingsController < ApplicationController

  def index
    @listings = Listing.order('created_at desc').page(params[:page]).per_page(60)
    @search = Listing.search do
      fulltext params[:search]
      facet(:marker_list, :limit => 48, :sort => :count)
      with(:marker_list, params[:tag]) if params[:tag].present?
      facet(:price) do
        row("$0 - $25") do
          with(:price, 0.00..25.00)
        end
        row("$25 - $75") do
          with(:price, 25.01..75.00)
        end
        row("$75 - $250") do
          with(:price, 75.01..250.00)
        end
        row("$250+") do
          with(:price).greater_than(250.00)
        end
      end
      with(:price, params[:price]) if params[:price].present?
    end
    @query = params[:search]
    @facet = params[:tag]
    @price_facet = params[:price]
    @results = @search.results

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @listings }
    end
  end

end 

查看

<% for row in @search.facet(:price).rows %>
    <span class="eloc"><%= link_to row.value, :price => row.value %></span>
<% end %> 

1 个答案:

答案 0 :(得分:2)

您可能已经找到了解决此问题的方法,我可以找到的解决方法之一是使用范围构面而不是查询构面。所以它会是这样的:

facet :price, :range => 0..300, :range_interval => 50
with(:price, Range.new(*params[:price].first.split("..").map(&:to_i))) if params[:price].present?  

希望它有所帮助!