Created
March 28, 2012 21:31
-
-
Save mrdanadams/2230763 to your computer and use it in GitHub Desktop.
Using Sunspot, Websolr, and Solr on Heroku
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
# Solr faking. | |
# see http://opensoul.org/blog/archives/2010/04/07/cucumber-and-sunspot/ | |
# Note: we need to do more than this if we start doing search tests in cucumber | |
$original_sunspot_session = Sunspot.session | |
Before("~@search") do | |
Sunspot.session = Sunspot::Rails::StubSessionProxy.new($original_sunspot_session) | |
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
gem 'sunspot_rails' | |
gem 'kaminari' | |
gem 'sunspot_with_kaminari' |
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
<h3 class="summary"><%= t('.your-search-header'), start: @start, end: @end, total: pluralize(@total, 'result') %></h3> | |
<% @results.each do |p| %> | |
<div class="result"><!-- ... --></div> | |
<% end %> | |
<%= paginate @search, window: 1 %> |
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
RSpec.configure do |c| | |
c.before :suite do | |
module Sunspot | |
def self.stub_session | |
@sub_session ||= Sunspot::Rails::StubSessionProxy.new self.session | |
end | |
end | |
end | |
c.before :each do | |
Sunspot.session = Sunspot.stub_session | |
Sunspot.session = Sunspot.session.original_session if example.metadata[:solr] | |
Sunspot.remove_all! | |
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
bundle exec rake sunspot:solr:start | |
bundle exec rake sunspot:solr:stop |
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 SearchController < ApplicationController | |
def search | |
page_size = 10 | |
@search = Profile.solr_search do | |
keywords params[:q] do | |
boost_fields name: 10.0, user: 4.0 | |
end | |
with :include_search, true | |
paginate page: params[:page], per_page: page_size | |
end | |
@results = @search.results | |
@total = @search.total | |
@paging = @total > page_size | |
@start = (@search.current_page - 1) * page_size + 1 | |
@end = [@start + page_size - 1, @total].min | |
render (if @results.empty? then :no_results else :results end) | |
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
describe 'Search' do | |
def commit | |
Sunspot.commit | |
end | |
def search(q) | |
visit "/search?q=#{q}" | |
end | |
it 'does something interesting', :solr do | |
Something.create! | |
commit | |
search 'foobar' | |
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
# Note: ENV['SOLR_URL'] and ENV['WEBSOLR_URL'] override any values here | |
# The defaults are same as what's below so you only need to override as desired. | |
# Removed production since that should never be used in heroku. | |
# See Sunspot::Rails::Configuration.solr_url | |
development: | |
solr: | |
hostname: localhost | |
port: 8982 | |
log_level: INFO | |
test: | |
solr: | |
hostname: localhost | |
port: 8981 | |
log_level: WARNING |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi where can I find the article associated with this code ?