Skip to content

Commit

Permalink
Full-text search is completely separated from the database search. Be…
Browse files Browse the repository at this point in the history
… careful when configuring Solr to return only the desired products.
  • Loading branch information
romul committed Dec 4, 2012
1 parent 48473f6 commit 4c6c331
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/models/spree/product_decorator.rb
@@ -1,5 +1,5 @@
Spree::Product.class_eval do 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 def taxon_ids
taxons.map(&:id) taxons.map(&:id)
Expand Down
6 changes: 6 additions & 0 deletions app/overrides/show_search_partials.rb
Expand Up @@ -9,3 +9,9 @@
:insert_top => "[data-hook='search_results']", :insert_top => "[data-hook='search_results']",
:partial => "spree/products/suggestion", :partial => "spree/products/suggestion",
:disabled => false) :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)
29 changes: 21 additions & 8 deletions lib/spree/search/solr.rb
@@ -1,20 +1,29 @@
module Spree::Search module Spree::Search
class Solr < defined?(Spree::Core::Search::MultiDomain) ? Spree::Core::Search::MultiDomain : Spree::Core::Search::Base 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 protected


# NOTE: This class seems to loaded and init'd on rails startup # 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. # 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 # the order option does not work... it generates the solr query request correctly
# but the returned result.records are not ordered 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 # 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] if not @properties[:sort].nil? and PRODUCT_SORT_FIELDS.has_key? @properties[:sort]
sort_option = PRODUCT_SORT_FIELDS[@properties[:sort]] sort_option = PRODUCT_SORT_FIELDS[@properties[:sort]]
base_scope = base_scope.order("#{sort_option[0]} #{sort_option[1].upcase}")
end end


# Solr query parameters: http://wiki.apache.org/solr/CommonQueryParameters # Solr query parameters: http://wiki.apache.org/solr/CommonQueryParameters
Expand All @@ -38,14 +47,18 @@ def get_products_conditions_for(base_scope, query)
:facets => facets, :facets => facets,
:limit => 25000, :limit => 25000,
:lazy => true, :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) 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[:products] = products

@properties[:suggest] = nil @properties[:suggest] = nil
begin begin
if suggest = result.suggest if suggest = result.suggest
Expand All @@ -56,7 +69,7 @@ def get_products_conditions_for(base_scope, query)
end end


@properties[:available_facets] = parse_facets_hash(result.facets) @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 end


def prepare(params) def prepare(params)
Expand All @@ -78,7 +91,7 @@ def parse_facets_hash(facets_hash = {"facet_fields" => {}})
next if options.size <= 1 next if options.size <= 1
facet = Facet.new(name.sub('_facet', '')) facet = Facet.new(name.sub('_facet', ''))
options.each do |value, count| 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 end
facets << facet facets << facet
end end
Expand Down

0 comments on commit 4c6c331

Please sign in to comment.