Skip to content

Instantly share code, notes, and snippets.

@radiospiel
Created September 13, 2011 10:22
Show Gist options
  • Save radiospiel/1213545 to your computer and use it in GitHub Desktop.
Save radiospiel/1213545 to your computer and use it in GitHub Desktop.
ThinkingSphinx: facetted search for ids only
class ThinkingSphinx::FacetIdsSearch < ThinkingSphinx::FacetSearch
def initialize(*args)
options = args.extract_options!
@fast_facet_names = options.delete(:facets) || raise("Missing facet names")
@fast_facet_names = [ @fast_facet_names ].flatten.map(&:to_s)
args.push options
super *args
end
def populate
ThinkingSphinx::Search.bundle_searches(@fast_facet_names) { |sphinx, name|
sphinx.search *(args + [facet_search_options(name)])
}.
each_with_index { |search, index|
facet_result = search.results[:matches].inject({}) do |hash, match|
attrs = match[:attributes]
doc, count = attrs["@groupby"], attrs["@count"]
hash.update doc => count
end
self[@fast_facet_names[index]] = facet_result
}
end
def facet_search_options(facet_name)
limit = max_matches
options.dup.update :group_function => :attr,
:limit => limit,
:max_matches => limit,
:page => 1,
:group_by => facet_name,
:ids_only => true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment