Permalink
Browse files

Full-text search is completely separated from the database search. Be…

… careful when configuring Solr to return only the desired products.
  • Loading branch information...
1 parent 48473f6 commit 4c6c3310dba4a1afe2dd9d511f6dc7c99d9c1a89 @romul committed Dec 4, 2012
Showing with 28 additions and 9 deletions.
  1. +1 −1 app/models/spree/product_decorator.rb
  2. +6 −0 app/overrides/show_search_partials.rb
  3. +21 −8 lib/spree/search/solr.rb
@@ -1,5 +1,5 @@
Spree::Product.class_eval do
- acts_as_solr :fields => PRODUCT_SOLR_FIELDS, :facets => PRODUCT_SOLR_FACETS rescue nil
+ acts_as_solr :fields => PRODUCT_SOLR_FIELDS, :facets => PRODUCT_SOLR_FACETS
def taxon_ids
taxons.map(&:id)
@@ -9,3 +9,9 @@
:insert_top => "[data-hook='search_results']",
:partial => "spree/products/suggestion",
:disabled => false)
+
+Deface::Override.new(:virtual_path => "spree/shared/_products",
+ :name => "override_paginated_products_var",
+ :insert_top => "h6.search-results-title",
+ :text => "<% paginated_products = @searcher.products %>",
+ :disabled => false)
View
@@ -1,20 +1,29 @@
module Spree::Search
class Solr < defined?(Spree::Core::Search::MultiDomain) ? Spree::Core::Search::MultiDomain : Spree::Core::Search::Base
+
+ def retrieve_products
+ if keywords.present?
+ find_products_by_solr(keywords)
+ else
+ @products_scope = get_base_scope
+ @products_scope.includes([:master]).page(page).per(per_page)
+ end
+ end
+
protected
# NOTE: This class seems to loaded and init'd on rails startup
# this means that any changes to the code will not take effect until the rails app is reloaded.
- def get_products_conditions_for(base_scope, query)
+ def find_products_by_solr(query)
# the order option does not work... it generates the solr query request correctly
# but the returned result.records are not ordered correctly
- # search_options.merge!(:order => (order_by_price == 'descend') ? "price desc" : "price asc")
+ # search_options.merge!(:sort => (order_by_price == 'descend') ? "price desc" : "price asc")
# TODO: find a better place to put the PRODUCT_SORT_FIELDS instead of the global constant namespace
if not @properties[:sort].nil? and PRODUCT_SORT_FIELDS.has_key? @properties[:sort]
sort_option = PRODUCT_SORT_FIELDS[@properties[:sort]]
- base_scope = base_scope.order("#{sort_option[0]} #{sort_option[1].upcase}")
end
# Solr query parameters: http://wiki.apache.org/solr/CommonQueryParameters
@@ -38,14 +47,18 @@ def get_products_conditions_for(base_scope, query)
:facets => facets,
:limit => 25000,
:lazy => true,
- :filter_queries => filter_queries
+ :filter_queries => filter_queries,
+ :page => page,
+ :per_page => per_page
}
result = Spree::Product.find_by_solr(query || '', search_options)
- products = result.records
-
+ @count = result.total
+ @properties[:total_entries] = @count
+ products = Kaminari.paginate_array(result.records, :total_count => @count).page(page).per(per_page)
@properties[:products] = products
+
@properties[:suggest] = nil
begin
if suggest = result.suggest
@@ -56,7 +69,7 @@ def get_products_conditions_for(base_scope, query)
end
@properties[:available_facets] = parse_facets_hash(result.facets)
- base_scope.where(["spree_products.id IN (?)", products.map(&:id)])
+ Spree::Product.where("spree_products.id" => products.map(&:id))
end
def prepare(params)
@@ -78,7 +91,7 @@ def parse_facets_hash(facets_hash = {"facet_fields" => {}})
next if options.size <= 1
facet = Facet.new(name.sub('_facet', ''))
options.each do |value, count|
- facet.options << FacetOption.new(value, count, facet.name)
+ facet.options << FacetOption.new(value, count, facet.name) if value.present?
end
facets << facet
end

0 comments on commit 4c6c331

Please sign in to comment.