Facets

31 views
Skip to first unread message

Tom Davies

unread,
May 13, 2008, 9:16:33 AM5/13/08
to Thinking Sphinx
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

Pat Allan

unread,
May 13, 2008, 9:21:49 AM5/13/08
to thinkin...@googlegroups.com
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

Tom Davies

unread,
May 13, 2008, 9:41:23 AM5/13/08
to Thinking Sphinx
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
Reply all
Reply to author
Forward
0 new messages