Created
August 26, 2010 18:55
-
-
Save nz/551970 to your computer and use it in GitHub Desktop.
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
# Denormalized multi-valued indexing with Solr and Sunspot | |
class Company | |
has_many :services | |
has_many :products | |
searchable do | |
text :name | |
text :description | |
string :service_names, :multiple => true | |
string :product_names, :multiple => true | |
end | |
def service_names | |
services.collect(&:name) | |
end | |
def product_names | |
products.collect(&:name) | |
end | |
# this record will be indexed a lot - best to queue it with DJ | |
handle_asynchronously :solr_index | |
end | |
class Product | |
has_and_belongs_to_many :companies # or a has_many :through, or a belongs_to, whatever... | |
after_create :update_companies # or update_company, if a belongs_to... | |
def update_companies | |
companies.map(&:solr_index) | |
end | |
end | |
class Service | |
has_and_belongs_to_many :companies # or a has_many :through, or a belongs_to, whatever... | |
after_create :update_companies # or update_company, if a belongs_to... | |
def update_companies | |
companies.map(&:solr_index) | |
end | |
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
class CompaniesController < ApplicationController | |
def search | |
@search = Company.search do | |
keywords params[:q] | |
with(:service_names, params[:service_names]) # e.g., ["testing", "marketing", "plumbing", ...] | |
with(:product_names, params[:product_names]) | |
end | |
@companies = Search.results | |
end | |
# ... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment