Great. I looked at the other code and it looks pretty similar.
One difference is that my function supports passing in multiple
attributes to facet:
> ThinkingSphinx::Search.facets('things', ['color', 'size'])
{'color' => {1 => 88, 2 => 12}, 'size' => {1 => 33, 4 => 66}}
Versus:
> ThinkingSphinx::Search.faceted_search('things', :group_by => 'color')
{1 => 88, 2 => 12}
> ThinkingSphinx::Search.faceted_search('things', :group_by => 'size')
{1 => 33, 4 => 66}
Also, it looks like Patrick's code supports faceting by class? Not
100% sure on how that is used.
Tom
On May 13, 9:21 am, Pat Allan <p...@freelancing-gods.com> wrote:
> Hi Tom
> 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.
> It's also worth knowing that Patrick Lenz has at least begun adding
> facets as well - his fork on github is:http://github.com/scoop/thinking-sphinx/tree/master
> Whether it's complete or not, or similar to yours, I've no idea.
> Again, something to analyse in the morning.
> Thanks again
> --
> Pat
> e: p...@freelancing-gods.com || m: 0413 273 337
> w:http://freelancing-gods.com|| p: 03 9386 0928
> discworld:http://ausdwcon.org|| skype: patallan
> On 13/05/2008, at 11:16 PM, Tom Davies wrote:
> > 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