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’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.
- sunspot: Sunspot is an all in one solution for using solr with a ruby project. It even uses rsolr under the hood.
Generally, I like API access layers to be as similar to the raw api as possible. Sunspot’s api works using a search block:
Post.search do
fulltext 'best pizza'
with :blog_id, 1
with(:published_at).less_than Time.now
order_by :published_at, :desc
paginate :page => 2, :per_page => 15
facet :category_ids, :author_id
end
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’s style of access because it’s most similar to http requests:
response = solr.get 'select', :params => {
:q=>'washington',
:start=>0,
:rows=>10
}
Solrsan 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.
Indexing
To index objects, edit config/solr/conf/schema.xml to state the types of fields you want to index. Or you can use dynamic fields to avoid specifying new fields each time.
Then, include Solrsan::Search into your model(Activerecord, mongoid, etc) and define a method called as_solr_document which returns a hash of the key-value pair entries to index. See the README for more examples.
You can add an after_save 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.
Searching
Search is as easy as:
response = Document.search(:q => "hello world")
This will return a hashmap response composed of docs and metadata. response[:docs] will be a will_paginated object collection and response[:metadata] contains various supporting items such as error messages, facets, etc.
Summary
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.
If you are interesting in using solrsan, the important links are the readme and unit tests. I’m already using solrsan on a few projects but it is still relatively new. Feel free to email/pull request any problems/bugs!