Skip to content

Instantly share code, notes, and snippets.

@mrdanadams
Created March 28, 2012 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdanadams/2230763 to your computer and use it in GitHub Desktop.
Save mrdanadams/2230763 to your computer and use it in GitHub Desktop.
Using Sunspot, Websolr, and Solr on Heroku
# 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
gem 'sunspot_rails'
gem 'kaminari'
gem 'sunspot_with_kaminari'
<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 %>
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
bundle exec rake sunspot:solr:start
bundle exec rake sunspot:solr:stop
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
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
# 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
@CrediAditya
Copy link

CrediAditya commented Dec 6, 2019

Hi where can I find the article associated with this code ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment