User:Paramdinkar1/sandbox
Introduction
[edit]To integrate the Apache-Solr search engine into Ruby and Rails, the Sunspot library provides an interactive technique. Sunspot is built on top of the RSolr library, which provides a low-level interface for Solr interaction; Sunspot manages the configuration of persistent Ruby classes for search and indexing and exposes Solr's most powerful features through a collection of DSLs. Complex search operations can be performed without hand-writing any boolean queries or building Solr parameters by hand. Sunspot is designed to be easily plugged into any Object-relational mapping, or even non-database-backed objects such as the filesystem. Sunspot is distributed under the MIT License.
Installation
[edit]Sunspot is easily configurable with RubyGems, type in the following command in Gems.
$ gem install sunspot
If you use an alternate system of package management, you can pull Sunspot from git://github.com/outoftime/sunspot.git .
Getting Started
[edit]Sunspot comes with a prepackaged instance of Solr, which makes it easy to get started in development mode. To start Sunspot’s Solr instance, run:
$ gem install sunspot_solr # not required for Sunspot < 1.3.0 $ sunspot-solr start
[1] This will store the index data in your operating system’s temporary directory – see Configuring Solr for use with Sunspot in development, testing, and production for more options available when when running the packaged Solr instance.
Configuration
[edit]Sunspot is configured using the Sunspot.config object. The following options are available:
Configuration Variable | Description | Default |
---|---|---|
Sunspot.config.solr.url | The URL at which to connect to Solr | http://127.0.0.1:8983/solr |
Sunspot.config.pagination.default_per_page | The number of results to return per page if not specified in the search | 30 |
Sunspot.config.indexing.default_batch_size | The number of records per batch to use when indexing | 50 |
Quickstart with Rails 3 / 4
[edit]Add the following commands to existing Gemfile:
gem 'sunspot_rails' gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development[1]
Bundle it!
bundle install[1]
Generate a default configuration file using the following command:
rails generate sunspot_rails:install[1]
If sunspot_solr
was installed, start the packaged Solr distribution with:
bundle exec rake sunspot:solr:start # or sunspot:solr:run to start in foreground[1]
Setting Up Objects
[edit]Add a searchable
block to the objects you wish to index.
class Post < ActiveRecord::Base searchable do text :title, :body text :comments do comments.map { |comment| comment.body } end boolean :featured integer :blog_id integer :author_id integer :category_ids, :multiple => true double :average_rating time :published_at time :expired_at string :sort_title do title.downcase.gsub(/^(an?|the)/, '') end end end[1]
text
fields will be full-text searchable. Other fields (e.g., integer
and string
) can be used to scope queries.
Searching Objects
[edit]Post.search do fulltext 'best pizza' with :blog_id, 1 with(:published_at).less_than Time.now field_list :blog_id, :title order_by :published_at, :desc paginate :page => 2, :per_page => 15 facet :category_ids, :author_id end[1]
Search In Depth
[edit]Given an object Post
setup in earlier steps ...
Full Text
[edit]# All posts with a `text` field (:title, :body, or :comments) containing 'pizza' Post.search { fulltext 'pizza' } # Posts with pizza, scored higher if pizza appears in the title Post.search do fulltext 'pizza' do boost_fields :title => 2.0 end end # Posts with pizza, scored higher if featured Post.search do fulltext 'pizza' do boost(2.0) { with(:featured, true) } end end # Posts with pizza *only* in the title Post.search do fulltext 'pizza' do fields(:title) end end # Posts with pizza in the title (boosted) or in the body (not boosted) Post.search do fulltext 'pizza' do fields(:body, :title => 2.0) end end[1]
Development
[edit]Running Tests
[edit]sunspot
[edit]Install the required gem dependencies:
cd /path/to/sunspot/sunspot bundle install[1]
Start a Solr instance on port 8983:
bundle exec sunspot-solr start -p 8983 # or `bundle exec sunspot-solr run -p 8983` to run in foreground[1]
Run the tests:
bundle exec rake spec[1]
If desired, stop the Solr instance:
bundle exec sunspot-solr stop[1]
sunspot_rails
[edit]Install the gem dependencies for sunspot
:
cd /path/to/sunspot/sunspot bundle install[1]
Start a Solr instance on port 8983:
bundle exec sunspot-solr start -p 8983 # or `bundle exec sunspot-solr run -p 8983` to run in foreground[1]
Navigate to the sunspot_rails
directory:
cd ../sunspot_rails[1]
Run the tests:
rake spec # all Rails versions rake spec RAILS=3.1.1 # specific Rails version only[1]
[1]If desired, stop the Solr instance:
cd ../sunspot bundle exec sunspot-solr stop[1]
Generating Documentation
[edit]Install the yard
and redcarpet
gems:
$ gem install yard redcarpet[1]
Uninstall the rdiscount
gem, if installed:
$ gem uninstall rdiscount[1]
Generate the documentation from topmost directory:
$ yardoc -o docs */lib/**/*.rb - README.md[1]
See Also
[edit]- Using Sunspot, Websolr, and Solr on Heroku (mrdanadams)
- Full Text Searching with Solr and Sunspot (Collective Idea)
- Full-text search in Rails with Sunspot (Tropical Software Observations)
- Sunspot: A Solr-Powered Search Engine for Ruby (Linux Magazine)
- Sunspot Showed Me the Light (ben koonse)
- RubyGems.org — A case study in upgrading to full-text search (Websolr)
- How to Implement Spatial Search with Sunspot and Solr (Code Quest)
- Sunspot 1.2 with Spatial Solr Plugin 2.0 (joelmats)
- heroku + websolr + sunspot (Websolr guides)
- How to get full text search working with Sunspot (Hobo Cookbook)
- Full text search with Sunspot in Rails (hemju)
- Using Sunspot for Free-Text Search with Redis (While I Pondered...)
- Default scope with Sunspot (Cloudspace)
- Index External Models with Sunspot/Solr (Medihack)
- Testing with Sunspot and Cucumber (Collective Idea)
- Solr, and Sunspot (YT!)
- Conditional Indexing with Sunspot (mikepack)
- Introduction to Full Text Search for Rails Developers (Valve's)
References
[edit]- Ruby on Rails official website – rubyonrails.org
- http://sunspot.github.com/ – Solr-powered search for Ruby objects
- https://github.com/rsolr/rsolr - A Ruby client for Apache Solr
- The Rails Tutorial – The Ruby on Rails Tutorial book and screencast series teach you how to develop and deploy Ruby on Rails apps
- Rails source code