I just installed the thinking-sphinx gem for rails yesterday and
everything is working fine, however there is one piece of
functionality that I couldn't seem to find how to accomplish. Here is
my scenario.
I have a product table that I'm searching on and each product has a
style_id, two or more products will often share the same style_id
which basically means they are the same product but could be a
different color, size, etc..
My index and search command are as follows
@products = Product.search params[:term], :group_by =>
'style_id', :group_function => :attr, :group_clause => "@weight
desc", :match_mode => :extended, :per_page => 100
That part of the search works great, the grouping works correctly and
it only shows one style of each product returned.
I have a 'Facet' defined in my index on the 'price' field so I can
show how many products fall under each price.
My problem comes now when I get those facets after the initial search;
facets = @products.facets
The prices group correctly, however the 'count' of the products that
fall under each 'price' is including the count for all of the
different styles, (remember I'm only showing one product per style
because they are essential all the same) so I can't say there are 10
products in the users search results that are selling for $20 when
they look down and only see 3 products for $20. Anyway, hope your
still with me on this. I couldn't find any combination of options to
pass to @products.facets to return what I was looking for. I know I
could accomplish what I need just by looping through the initial
results from the search and creating my own hash VS using facets, but
that's no fun. Hopefully I just missed something here, for now I've
decided to accomplish this by just overriding the "add_from_results"
method in the FacetSearch class like below, and passing in the
following options.
facets = @products.facets(:group => 'p_style_id', :count_by_groups =>
true)
module ThinkingSphinx
class FacetSearch < Hash
def add_from_results(facet, search)
name = ThinkingSphinx::Facet.name_for(facet)
facet = facet_from_name(facet)
self[name] ||= {}
return if search.empty?
search.each_with_match do |result, match|
facet_value = facet.value(result, match[:attributes])
self[name][facet_value] ||= 0
self[name][facet_value] += @options[:count_by_groups] ? 1 :
match[:attributes]["@count"]
end
end
end
end
Thanks for the feedbak.
--
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en.