Skip to content

Commit

Permalink
Added ability to configure the list of indexable fields and facets.
Browse files Browse the repository at this point in the history
  • Loading branch information
romul committed Feb 27, 2011
1 parent 2dd36b8 commit 3c578ba
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
23 changes: 6 additions & 17 deletions app/models/product_decorator.rb
@@ -1,9 +1,5 @@
Product.class_eval do Product.class_eval do
acts_as_solr :fields => [:name, :description, :is_active, {:price => :float}, acts_as_solr :fields => PRODUCT_SOLR_FIELDS, :facets => PRODUCT_SOLR_FACETS
:taxon_ids, :price_range, :taxon_names, :store_ids,
:brand_property, :color_option, :size_option],
:facets=>[:price_range, :taxon_names,
:brand_property, :color_option, :size_option]


def taxon_ids def taxon_ids
taxons.map(&:id) taxons.map(&:id)
Expand All @@ -30,19 +26,12 @@ def taxon_names
end end


def price_range def price_range
price_ranges = YAML.load(Spree::Config[:product_price_ranges]) max = 0
case price PRODUCT_PRICE_RANGES.each do |range, name|
when 0..25 return name if range.include?(price)
price_ranges[0] max = range.max if range.max > max
when 25..50
price_ranges[1]
when 50..100
price_ranges[2]
when 100..200
price_ranges[3]
else
price_ranges[4]
end end
I18n.t(:price_and_above, :price => max)
end end


def brand_property def brand_property
Expand Down
8 changes: 6 additions & 2 deletions app/views/products/_facets.html.erb
@@ -1,9 +1,13 @@
<% <%
facets = @searcher.facets || [] facets = @searcher.facets || []
taxon_names = @taxon ? @taxon.self_and_descendants.map(&:name) : [] taxon_names = @taxon ? @taxon.self_and_descendants.map(&:name) : []
for facet in facets for facet in facets
options = facet.options options = facet.options
options = options.sort{|x,y| y.count <=> x.count} unless facet.name == "price_range" if facet.name == "price_range"
options = options.sort{|x, y| x.name <=> y.name}
else
options = options.sort{|x, y| y.count <=> x.count}
end
unless options.empty? %> unless options.empty? %>
<h4><%= t "#{facet.name}_facet" %></h4> <h4><%= t "#{facet.name}_facet" %></h4>
<ul><% <ul><%
Expand Down
13 changes: 13 additions & 0 deletions config/initializers/solr_config.rb
@@ -0,0 +1,13 @@
unless defined?(PRODUCT_PRICE_RANGES)
PRODUCT_PRICE_RANGES = {0..25 => " Under $25", 25..50 => " $25 to $50",
50..100 => " $50 to $100", 100..200 => "$100 to $200"}
end
unless defined?(PRODUCT_SOLR_FIELDS)
PRODUCT_SOLR_FIELDS = [:name, :description, :is_active, {:price => :float},
:taxon_ids, :price_range, :taxon_names, :store_ids,
:brand_property, :color_option, :size_option]
end
unless defined?(PRODUCT_SOLR_FACETS)
PRODUCT_SOLR_FACETS = [:price_range, :taxon_names,
:brand_property, :color_option, :size_option]
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Expand Up @@ -4,3 +4,4 @@ en:
brand_property_facet: "Brand" brand_property_facet: "Brand"
color_option_facet: "Color" color_option_facet: "Color"
size_option_facet: "Size" size_option_facet: "Size"
price_and_above: "$%{price} and above"
1 change: 1 addition & 0 deletions config/locales/ru.yml
Expand Up @@ -4,3 +4,4 @@ ru:
brand_property_facet: "Бренд" brand_property_facet: "Бренд"
color_option_facet: "Цвет" color_option_facet: "Цвет"
size_option_facet: "Размер" size_option_facet: "Размер"
price_and_above: "%{price} руб. и выше"
14 changes: 3 additions & 11 deletions lib/spree/search/solr.rb
Expand Up @@ -4,7 +4,7 @@ class Solr < defined?(Spree::Search::MultiDomain) ? Spree::Search::MultiDomain :


def get_products_conditions_for(base_scope, query) def get_products_conditions_for(base_scope, query)
facets = { facets = {
:fields => [:price_range, :taxon_names, :brand_property, :color_option, :size_option], :fields => PRODUCT_SOLR_FACETS,
:browse => @properties[:facets_hash].map{|k,v| "#{k}:#{v}"}, :browse => @properties[:facets_hash].map{|k,v| "#{k}:#{v}"},
:zeros => false :zeros => false
} }
Expand Down Expand Up @@ -50,20 +50,12 @@ def prepare(params)


def parse_facets_hash(facets_hash = {"facet_fields" => {}}) def parse_facets_hash(facets_hash = {"facet_fields" => {}})
facets = [] facets = []
price_ranges = YAML::load(Spree::Config[:product_price_ranges])
facets_hash["facet_fields"].each do |name, options| facets_hash["facet_fields"].each do |name, options|
options = Hash[*options.flatten] if options.is_a?(Array) options = Hash[*options.flatten] if options.is_a?(Array)
next if options.size <= 1 next if options.size <= 1
facet = Facet.new(name.sub('_facet', '')) facet = Facet.new(name.sub('_facet', ''))
if name == 'price_range_facet' options.each do |value, count|
price_ranges.each do |price_range| facet.options << FacetOption.new(value, count)
count = options[price_range]
facet.options << FacetOption.new(price_range, count) if count
end
else
options.each do |value, count|
facet.options << FacetOption.new(value, count)
end
end end
facets << facet facets << facet
end end
Expand Down
2 changes: 0 additions & 2 deletions lib/spree_solr_search.rb
Expand Up @@ -9,8 +9,6 @@ def self.activate


if Spree::Config.instance if Spree::Config.instance
Spree::Config.searcher_class = Spree::Search::Solr Spree::Config.searcher_class = Spree::Search::Solr
Spree::Config.set(:product_price_ranges =>
["Under $25", "$25 to $50", "$50 to $100", "$100 to $200", "$200 and above"])
end end


Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c| Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Expand Down

0 comments on commit 3c578ba

Please sign in to comment.