Last active
August 29, 2015 14:02
-
-
Save kenton/58d48d2ab80e20106ce5 to your computer and use it in GitHub Desktop.
problem with solr joins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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