Skip to content

Instantly share code, notes, and snippets.

@clemens
Created February 4, 2011 10:40
Show Gist options
  • Select an option

  • Save clemens/810978 to your computer and use it in GitHub Desktop.

Select an option

Save clemens/810978 to your computer and use it in GitHub Desktop.
a little hack
def facets(facets, options = {})
return {} if facets.blank?
only_roots = options.key?(:only_roots) ? options.delete(:only_roots) : true # only use roots for facets by default
facets.inject(ActiveSupport::OrderedHash.new) do |category_facets, (category_ids_string, count)|
category_ids = category_ids_string.split(',').map(&:to_i)
category_ids = category_ids[0,1] if only_roots # only use root element if we want only roots
category_ids.each_with_index do |category_id, i|
category_facets[category_id] ||= Facet.new(category_id, 0)
category_facets[category_id].add_count(count)
end
category_facets
end
end
define_index do
indexes :name
indexes description
indexes brand_name
indexes tags.name, :as => :tag_list
indexes merchant.name, :as => :merchant_name
indexes category.path, :as => :category_names # keeping it simple
has merchant_id, :facet => true
has active, :type => :boolean
has "CRC32(`brand_name`)", :as => :brand, :type => :integer, :facet => true
# using string attributes for filtering is not yet functional in Sphinx 1.10 beta
# however, we could do it the same way as with categories ...
# has brand_name, :as => :brand, :type => :string, :facet => true
has category.path_ints, :as => :category_ids, :type => :multi
has category.path_ints, :as => :category_path, :type => :string, :facet => true
set_property :field_weights => {
:category_names => 10,
:name => 5
}
end
module ThinkingSphinx
Attribute.class_eval do
def type_to_config
{
:multi => :sql_attr_multi,
:datetime => :sql_attr_timestamp,
# :string => :sql_attr_str2ordinal,
:string => :sql_attr_string, # works for Sphinx >= 1.10 beta
:float => :sql_attr_float,
:boolean => :sql_attr_bool,
:integer => :sql_attr_uint,
:bigint => :sql_attr_bigint
}[type]
end
end
end
# We need to avoid category_path facets being translated because this always
# instantiates shitloads of products => slow as hell.
# NOTE: commenting this out changes nothing – the error still happens ...
module ThinkingSphinx
Facet.class_eval do
def value(object, attribute_hash)
attribute_value = attribute_hash['@groupby']
case column.__name
when :path_ints
return attribute_hash['category_path'] if attribute_hash.key?('category_path')
end
return translate(object, attribute_value) if translate? || float?
case @property.type
when :datetime
Time.at(attribute_value)
when :boolean
attribute_value > 0
else
attribute_value
end
end
end
FacetSearch.class_eval do
def translate?(name)
return false if %w(category_path_facet).include?(name)
facet = facet_from_name(name)
facet.translate? || facet.float?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment