Skip to content

Instantly share code, notes, and snippets.

@kenton
Last active August 29, 2015 14:02
Show Gist options
  • Save kenton/58d48d2ab80e20106ce5 to your computer and use it in GitHub Desktop.
Save kenton/58d48d2ab80e20106ce5 to your computer and use it in GitHub Desktop.
problem with solr joins
# Notes:
# a product has_many variants
# conf is just a configuration object that holds a bunch of stuff. in this case, we're interested in the values
# for option_facets, which are the values we want to be able to facet on.
searchable do
if conf.option_facets.present?
conf.option_facets.each do |option_facet|
join(option_facet, :type => :integer, :join_string => "from=product_id to=id")
end
end
end
# And then my search looks like this:
Product.search do
with(:frame_design, 195)
end
# for each entry in option_facets, we want to store the IDs that correspond to that item
# so for the entry :frame_design, we want to store the IDs for the option_values related to "Frame Design" (e.g., standard, wide, etc)
searchable do
if conf.option_facets.present?
conf.option_facets.each do |option_facet|
option_type_name = option_facet.to_s.titleize
integer option_facet, :multiple => true, :references => OptionValue do
option_value_ids_for_types([option_type_name])
end
end
end
integer :product_id
end
# finds all the option_values that match the names in "type_array"
# returns an array of the IDs for those option_values
def option_value_ids_for_types(type_array)
return [] if type_array.nil? || type_array.empty?
matching_option_values = option_values.select { |ov| type_array.include?(ov.option_type.name) }
matching_option_values.map(&:id).flatten
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment