<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spiral_code &#187; sunspot</title>
	<atom:link href="http://blog.trydionel.com/tag/sunspot/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.trydionel.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Aug 2010 00:35:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Testing Sunspot with Cucumber</title>
		<link>http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=testing-sunspot-with-cucumber</link>
		<comments>http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 19:07:43 +0000</pubDate>
		<dc:creator>Jeff Tucker</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[pickle]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sunspot]]></category>

		<guid isPermaLink="false">http://blog.trydionel.com/?p=124</guid>
		<description><![CDATA[I&#8217;ve been using Sunspot as my full-text search provider for a few months now. It&#8217;s a very enjoyable library &#8212; it hooks itself into Rails, provides an extension to disable itself in RSpec and even throws in a few convenient &#8230; <a href="http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using Sunspot as my full-text search provider for <a href="http://blog.trydionel.com/2009/11/19/a-few-sunspot-tips/">a few months now</a>.  It&#8217;s a very enjoyable library &#8212; it hooks itself into Rails, provides an extension to disable itself in RSpec and even throws in a few convenient rake tasks.  This fun came to a quick end, though, when I started delving into Cucumber again.  Any feature involving finding a Sunspot-enabled item failed completely, but without any helpful errors.  After a shameful amount of time banging my head against the desk, I finally got everyone to play nicely.</p>
<h2>The Code</h2>
<p>Let&#8217;s just drop the code in and discuss it afterwards:</p>
<pre class="brush: ruby;">
# config/sunspot.yml
# copy the test settings into the cucumber environment
test: &amp;TEST
  solr:
    hostname: localhost
    port: 8981
    log_level: WARNING

cucumber:
  &lt;&lt;: *TEST

# features/support/config.rb
# start the Solr server and give it a few seconds to initialize
Sunspot::Rails::Server.start
sleep 5

# make sure that pickle calls #index! on our appropriate models
require File.join(File.dirname(__FILE__), 'pickle')
module Pickle
  module Session

    def create_model_with_sunspot(a_model_name, fields = nil)
      result = create_model_without_sunspot(a_model_name, fields)

      model = model(a_model_name)
      model.index! if model.respond_to?(:index!)

      result
    end
    alias_method_chain :create_model, :sunspot

  end
end

# clean out the Solr index after each scenario
After do
  Post.remove_all_from_index!
end

# shut down the Solr server
at_exit do
  Sunspot::Rails::Server.stop
end
</pre>
</p>
<h2>The Details</h2>
<p>This method was heavily influenced by <a href="http://opensoul.org/2009/6/1/cucumber-scenarios-that-depend-on-sphinx">Brandon Keeper&#8217;s post on using Cucumber with ThinkingSphinx</a>.  Thanks Brandon!  So in my setup, I&#8217;m using Sunspot, Cucumber and Pickle (see Ryan Bates&#8217; <a href="http://railscasts.com/episodes/186-pickle-with-cucumber">railscast on using Pickle with Cucumber</a> for more details on the wonderful Pickle gem).</p>
<p>First and foremost is getting the testing Solr server running.  It will look for settings in <code>config/sunspot.yml</code> according to the environment we&#8217;re running in, so we need to duplicate the test environment for cucumber.  I&#8217;ve found that it takes 3-5 seconds for Solr to properly spin up, so we&#8217;re <code>sleep</code>ing for a few seconds to avoid the problem.</p>
<p>Next we need to ensure that each model gets loaded into Solr&#8217;s index when created.  If you&#8217;re instantiating models on your own, this boils down to calling <code>#index!</code> on any models that you create.  Since I&#8217;m using Pickle (and didn&#8217;t want to rework the included <code>pickle_steps.rb</code>), I needed a bit more invasive measure.  A bit of <code>alias_method_chain</code> voodoo lets me call <code>#index!</code> on every instantiated model that responds to it.  This ensures that Pickle always adds it&#8217;s models to Solr&#8217;s index.  I was pleased to find that I didn&#8217;t have to disable transactional fixtures </p>
<p>Finally, we need to ensure that our index is fresh for each scenario, so we clear the Solr index out after each scenario is run.  If you want a more targeted approach, you can use</p>
<pre class="brush: ruby;">
# features/support/config.rb
# clear Solr index only if we've actually used it
After('@sunspot') do
  Post.remove_all_from_index!
end
</pre>
<p>to only clear the Solr index after stories which are tagged with <code>@sunspot</code>.  Once all the stories are run, shut down the server.  Viola!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.trydionel.com/2010/02/06/testing-sunspot-with-cucumber/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A Few SunSpot Tips</title>
		<link>http://blog.trydionel.com/2009/11/19/a-few-sunspot-tips/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-few-sunspot-tips</link>
		<comments>http://blog.trydionel.com/2009/11/19/a-few-sunspot-tips/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 15:09:37 +0000</pubDate>
		<dc:creator>Jeff Tucker</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[solr]]></category>
		<category><![CDATA[sunspot]]></category>

		<guid isPermaLink="false">http://trydionel.wordpress.com/?p=36</guid>
		<description><![CDATA[I started playing with <a href="http://github.com/outoftime/sunspot">SunSpot</a> yesterday with the intent of using it as my full-text search provider in a Rails application.  The few how-tos I found show the beautiful DSL it provides, but unfortunately not much more.  Below are a few gotchas I ran into while getting it to work on my setup. <a href="http://blog.trydionel.com/2009/11/19/a-few-sunspot-tips/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I started playing with <a href="http://github.com/outoftime/sunspot">SunSpot</a> yesterday with the intent of using it as my full-text search provider in a Rails application.  The few how-tos I found show the beautiful DSL it provides, but unfortunately not much more.  Below are a few gotchas I ran into while getting it to work on my setup.</p>
<h3>1. Sunspot includes a development version of Solr</h3>
<p>There&#8217;s no need to download an official Solr release from Apache for development work.  Just install <code>sunspot</code> and you get Solr for free, complete with some nice Rake tasks.<br />
<code><br />
gem install sunspot<br />
rake sunspot:solr:start --starts the solr server<br />
rake sunspot:solr:stop --stops the solr server<br />
</code></p>
<h3>2. Using Sunspot in Rails requires the sunspot_rails gem</h3>
<p>This one was easy to solve, but still surprised me a bit.  The <code>sunspot</code> gem only provides the base interaction with the <code>Solr</code> server.  In order to get <code>ActiveRecord</code> integration, you need to add the <a href="http://github.com/outoftime/sunspot_rails"><code>sunspot_rails</code> gem</a>:</p>
<pre class="brush: ruby;">
# config/environment.rb
config.gem 'sunspot', :lib =&amp;gt; 'sunspot'
config.gem 'sunspot_rails', :lib =&amp;gt; 'sunspot/rails'

$ rake gems:install
</pre>
<h3>3. Text Analysis is setup in Solr, not in Sunspot</h3>
<p>It seems there&#8217;s not much written in the Sunspot documentation/groups about text analysis (such as word stemming, stop word filters, synonyms analysis, etc), and for good reason.  These settings are defined in Solr&#8217;s setup via the <code>schema.xml</code> file.  More details on Solr&#8217;s text analysis abilities can be found on <a href="http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters">Apache&#8217;s website.</a>  If you&#8217;re using the install you get with Sunspot, you can find this in <code>RAILS_ROOT/solr/conf/schema.xml</code>.</p>
<h3>4. Sunspot executes its <code>search</code> DSL in a new scope</h3>
<p>This one took me forever to figure out.  By default, any details passed into Sunspot.search are not evaluated in the current context.  This means that trying to search directly from the controller just doesn&#8217;t work as I expected.  There&#8217;s no mention of this in sunspot_rails&#8217; documentation, but <a href="http://outoftime.github.com/sunspot/docs/classes/Sunspot.html#M000006">there&#8217;s a brief snippet in sunspot&#8217;s</a>:</p>
<blockquote><p>If the block passed to search takes an argument, that argument will present the DSL, and the block will be evaluated in the calling context. This will come in handy for building searches using instance data or methods</p></blockquote>
<p>So, in order to search from the controller (or rather in the model, like you should), you&#8217;ll need to pass an argument to the <code>Sunspot.search</code> block:</p>
<pre class="brush: ruby;">
@search = Sunspot.search(Post) do |query|
query.keywords params[:query]
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.trydionel.com/2009/11/19/a-few-sunspot-tips/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
