I just recently switched to ThinkingSphinx from Ultrasphinx (and
before that acts_as_sphinx). Ultrasphinx worked great but had just a
couple issues that ThinkingSphinx resolves. Most notably the multi-
valued field support. One feature of Ultrasphinx I ported over to
ThinkingSphinx is the ability to generate facets for your attributes.
It requires an additional sphinx call per attribute but in my
experience it is very fast.
I am posting this patch to the group in hopes that this could be
rolled into ThinkingSphinx or at least be useful for others. I am
currently loading this using a Rails initializer. Comments and
improvements are welcome:.
Here is the code for facets:
require 'thinking_sphinx'
module ThinkingSphinx
class Search
class << self
# Add facet support
def facets(query, attrs, options={})
options.merge!({:group_function => :attr, :group_clauses =>
"@count desc"})
attrs = [attrs] unless attrs.is_a? Array
attr_facets = {}
attrs.each do |attr|
options[:group_by]=attr
results, client = search_results(query, options)
facets = {}
results[:matches].each {|e| facets[e[:attributes]
['@groupby']]=e[:attributes]['@count']}
attr_facets[attr] = facets
end
attr_facets
end
end
end
end
Thanks for that code - I've had a couple of other people request the facets feature. When I'm more awake I'll actually pay attention to the code and try to wrap it in to trunk.
> I just recently switched to ThinkingSphinx from Ultrasphinx (and > before that acts_as_sphinx). Ultrasphinx worked great but had just a > couple issues that ThinkingSphinx resolves. Most notably the multi- > valued field support. One feature of Ultrasphinx I ported over to > ThinkingSphinx is the ability to generate facets for your attributes. > It requires an additional sphinx call per attribute but in my > experience it is very fast.
> I am posting this patch to the group in hopes that this could be > rolled into ThinkingSphinx or at least be useful for others. I am > currently loading this using a Rails initializer. Comments and > improvements are welcome:.
> Here is the code for facets:
> require 'thinking_sphinx' > module ThinkingSphinx > class Search > class << self > # Add facet support > def facets(query, attrs, options={}) > options.merge!({:group_function => :attr, :group_clauses => > "@count desc"}) > attrs = [attrs] unless attrs.is_a? Array > attr_facets = {} > attrs.each do |attr| > options[:group_by]=attr > results, client = search_results(query, options) > facets = {} > results[:matches].each {|e| facets[e[:attributes] > ['@groupby']]=e[:attributes]['@count']} > attr_facets[attr] = facets > end > attr_facets > end > end > end > end
> Thanks for that code - I've had a couple of other people request the
> facets feature. When I'm more awake I'll actually pay attention to the
> code and try to wrap it in to trunk.
> > I just recently switched to ThinkingSphinx from Ultrasphinx (and
> > before that acts_as_sphinx). Ultrasphinx worked great but had just a
> > couple issues that ThinkingSphinx resolves. Most notably the multi-
> > valued field support. One feature of Ultrasphinx I ported over to
> > ThinkingSphinx is the ability to generate facets for your attributes.
> > It requires an additional sphinx call per attribute but in my
> > experience it is very fast.
> > I am posting this patch to the group in hopes that this could be
> > rolled into ThinkingSphinx or at least be useful for others. I am
> > currently loading this using a Rails initializer. Comments and
> > improvements are welcome:.