Skip to content

Instantly share code, notes, and snippets.

@igal
Created March 2, 2011 02:08
Show Gist options
  • Save igal/850329 to your computer and use it in GitHub Desktop.
Save igal/850329 to your computer and use it in GitHub Desktop.
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