Question

I'm trying to set up price faceting on one of my models but I can't seem to get it to work properly. No matter which facet I select it only returns listings that's price equals $0. I'm not sure what I'm missing here. Any help would be much appreciated. Here's my code:

Migration

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

Model

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

Controller

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 

View

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

Solution

You might have already found a way around this, one of the workarounds i could find for this was to use the range facet instead of the query facet. SO it would be something like :

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

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top