Skip to content

Instantly share code, notes, and snippets.

@nz
Created August 26, 2010 18:55
Show Gist options
  • Save nz/551970 to your computer and use it in GitHub Desktop.
Save nz/551970 to your computer and use it in GitHub Desktop.
# 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
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