Skip to content

Instantly share code, notes, and snippets.

@igal
Created Mar 2, 2011
Embed
What would you like to do?
Sample Sunspot full-text search code
class Post < ActiveRecord::Base
# Define index
searchable do
text :title, :body
string :author_name
integer :blog_id
integer :category_ids
float :average_rating, :using => :ratings_average
time :published_at
string :sort_title do
title.downcase.sub(/^(an?|the)\W+/, ''/) if title = self.title
end
end
# Return records matching index
def search(query, author)
result = self.solr_search do
keywords query # e.g. 'great pizza'
with :author_name, author # e.g. 'Mark Twain'
with(:blog_id).any_of [2, 14]
with(:category_ids).all_of [4, 10]
with(:published_at).less_than Time.now
any_of do
with(:expired_at).greater_than(Time.now)
with(:expired_at, nil)
end
without :title, 'Bad Title'
without bad_instance # specifically exclude this instance from results
paginate :page => 3, :per_page => 15
order_by :average_rating, :desc
facet :blog_id
end
return result.results
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment