<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Tommy Chheng : Programming Blog &#187; Programming</title>
	<atom:link href="http://tommy.chheng.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://tommy.chheng.com</link>
	<description>Ruby, Scala, Data, Search and other Programming topics.</description>
	<lastBuildDate>Fri, 03 Feb 2012 20:55:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='tommy.chheng.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Tommy Chheng : Programming Blog &#187; Programming</title>
		<link>http://tommy.chheng.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://tommy.chheng.com/osd.xml" title="Tommy Chheng : Programming Blog" />
	<atom:link rel='hub' href='http://tommy.chheng.com/?pushpress=hub'/>
		<item>
		<title>Solrsan: Lightweight Solr Gem for Ruby on Rails 3 Applications</title>
		<link>http://tommy.chheng.com/2011/04/29/solrsan-lightweight-solr-gem-for-ruby-on-rails-3-applications/</link>
		<comments>http://tommy.chheng.com/2011/04/29/solrsan-lightweight-solr-gem-for-ruby-on-rails-3-applications/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 18:24:01 +0000</pubDate>
		<dc:creator>tommychheng</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tommy.chheng.com/?p=434</guid>
		<description><![CDATA[I decided to create Solrsan to use the Apache Solr search server in my various Rails 3 applications. Currently, there are two main ruby gems for using Apache Solr in a ruby project: rsolr: RSolr is a low level layer to Apache Solr. Because it&#8217;s meant to be just an access layer, rsolr is missing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tommy.chheng.com&amp;blog=21824714&amp;post=434&amp;subd=tommychheng&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I decided to create <a href="https://github.com/tc/solrsan">Solrsan</a> to use the Apache Solr search server in my various Rails 3 applications. Currently, there are two main ruby gems for using <a href="http://lucene.apache.org/solr/">Apache Solr</a> in a ruby project: </p>
<ul>
<li><a href="https://github.com/mwmitchell/rsolr.git">rsolr</a>: RSolr is a low level layer to Apache Solr. Because it&#8217;s meant to be just an access layer, rsolr is missing the configuration setup such as the schema.xml, solrconfig.xml, etc which is custom per each Ruby/Rails app.</li>
<li><a title="sunspot" href="http://outoftime.github.com/sunspot/">sunspot</a>:  Sunspot is an all in one solution for using solr with a ruby project. It even uses rsolr under the hood.</li>
</ul>
<p>Generally, I like API access layers to be as similar to the raw api as possible. Sunspot&#8217;s api works using a search block:<br />
<code></p>
<pre>
Post.search do
  fulltext 'best pizza'
  with :blog_id, 1
  with(:published_at).less_than Time.now
  order_by :published_at, :desc
  paginate :page =&gt; 2, :per_page =&gt; 15
  facet :category_ids, :author_id
end
</pre>
<p></code></p>
<p>The actual query becomes an http get request. Solr itself is just a Java servlet which just reads http requests and responses with json/xml/other formats. I prefer using rsolr&#8217;s style of access because it&#8217;s most similar to http requests:</p>
<p><code></p>
<pre>
  response = solr.get 'select', :params =&gt; {
    :q=&gt;'washington',
    :start=&gt;0,
    :rows=&gt;10
  }
</pre>
<p></code></p>
<p><a href="https://github.com/tc/solrsan">Solrsan</a> also uses rsolr under the hood and adds a few extra functionality. For example, when you want to add solr functionality to your ruby/rails app, you need its own set of config files, a way to start/stop the solr server, a way to deploy using capistrano.  Solrsan comes with these basic setup files to help you get started.</p>
<h3>Indexing</h3>
<p>To index objects, edit <code>config/solr/conf/schema.xml</code> to state the types of fields you want to index. Or you can use dynamic fields to avoid specifying new fields each time. </p>
<p>Then, <code>include Solrsan::Search</code> into your model(Activerecord, mongoid, etc) and define a method called <code>as_solr_document</code> which returns a hash of the key-value pair entries to index. See the <a href="https://github.com/tc/solrsan">README</a> for more examples. </p>
<p>You can add an <code>after_save</code> method to call the index method as well. I did not automatically add the index method on every object save since some systems may need index via a different method such as via a queuing system.</p>
<h3>Searching</h3>
<p>Search is as easy as:<br />
<code>  response = Document.search(:q =&gt; "hello world") </code><br />
This will return a hashmap response composed of docs and metadata. <code>response[:docs]</code> will be a will_paginated object collection and response[:metadata] contains various supporting items such as error messages, facets, etc.</p>
<h3>Summary</h3>
<p>So I decided not to use sunspot because I want a transparent API access over a DSL implementation and I needed something more than the basic rsolr gem.</p>
<p>If you are interesting in using solrsan, the important links are the readme and unit tests. I&#8217;m already using solrsan on a few projects but it is still relatively new. Feel free to email/pull request any problems/bugs!</p>
<ul>
<li><a href="https://github.com/tc/solrsan">Solrsan readme</a>: Setup guide</li>
<li><a href="https://github.com/tc/solrsan_example">Solrsan Rails 3 example</a></li>
<li><a href="https://github.com/tc/solrsan/blob/master/test/unit/search_test.rb">Solrsan tests</a>: examples of how to perform the different types of search queries</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tommychheng.wordpress.com/434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tommychheng.wordpress.com/434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tommychheng.wordpress.com/434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tommychheng.wordpress.com/434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tommychheng.wordpress.com/434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tommychheng.wordpress.com/434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tommychheng.wordpress.com/434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tommychheng.wordpress.com/434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tommychheng.wordpress.com/434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tommychheng.wordpress.com/434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tommychheng.wordpress.com/434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tommychheng.wordpress.com/434/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tommychheng.wordpress.com/434/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tommychheng.wordpress.com/434/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tommy.chheng.com&amp;blog=21824714&amp;post=434&amp;subd=tommychheng&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tommy.chheng.com/2011/04/29/solrsan-lightweight-solr-gem-for-ruby-on-rails-3-applications/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8c8f227adb759f3eb632bcfe77131b8e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tommychheng</media:title>
		</media:content>
	</item>
	</channel>
</rss>
