Sunspot is a Ruby library for expressive, powerful interaction with the Solr search engine. Sunspot is built on top of the solr-ruby gem, which provides a low-level interface for Solr interaction; Sunspot provides a simple, intuitive, expressive DSL backed by powerful features for indexing objects and searching against them.
Sunspot is designed to be easily plugged in to any ORM, or even non-database-backed objects such as the filesystem.
Define indexing strategy for each searchable class using intuitive block-based API
Clean separation between keyword-searchable fields and fields for scoping/ordering
Define fields based on existing attributes or “virtual fields” for custom indexing
Indexes each object's entire superclass hierarchy, for easy searching for all objects inheriting from a parent class
Intuitive DSL for scoping searches, with all the usual boolean operators available
Intuitive interface for requesting facets on indexed fields
Extensible adapter architecture for easy integration of other ORMs or non-model classes
Full compatibility with will_paginate
gem sources -a http://gems.github.com gem install outoftime-sunspot
In order to start the packaged Solr installation, run:
sunspot-solr start -- [-d /path/to/data/directory] [-p port]
If you don't specify a data directory, your Solr index will be stored in your operating system's temporary directory.
You can also run your own instance of Solr wherever you'd like; just copy the solr/config/schema.xml file out of the gem's solr into your installation. You can change the URL at which Sunspot accesses Solr with:
Sunspot.config.solr.url = 'http://solr.my.host:9818/solr'
The Sunspot::Rails plugin makes integrating Sunspot into Rails drop-in easy.
Define an index:
class Post #... end Sunspot.setup(Post) do text :title, :body string :author_name integer :blog_id integer :category_ids float :average_rating, :using => :ratings_average time :published_at string :sort_title do title.downcase.sub(/^(an?|the)\W+/, ''/) if title = self.title end end
See Sunspot.setup for more information.
Search for objects:
search = Sunspot.search Post do keywords 'great pizza' with :author_name, 'Mark Twain' with(:blog_id).any_of [2, 14] with(:category_ids).all_of [4, 10] with(:published_at).less_than Time.now without :title, 'Bad Title' without bad_instance # specifically exclude this instance from results paginate :page => 3, :per_page => 15 order_by :average_rating, :desc facet :blog_id end
See Sunspot.search for more information.
Get data from search:
search.results search.total search.page search.per_page search.facet(:blog_id)
About the API documentation
All of the methods documented in the RDoc are considered part of Sunspot's public API. Methods that are not part of the public API are documented in the code, but excluded from the RDoc. If you find yourself needing to access methods that are not part of the public API in order to do what you need, please contact me so I can rectify the situation!
Please submit bug reports to outoftime.lighthouseapp.com/projects/20339-sunspot
Posts about Sunspot from my tumblog: outofti.me/tagged/sunspot
Mat Brown <email@example.com>
(The MIT License)
Copyright © 2008-2009 Mat Brown
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.