User:Mhpandya/sandbox

Introduction
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
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
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 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
Sunspot is configured using the Sunspot.config object. The following options are available:

Quickstart with Rails 3 / 4
Add the following commands to existing Gemfile: gem 'sunspot_rails' gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development Bundle it! bundle install Generate a default configuration file using the following command: rails generate sunspot_rails:install If  was installed, start the packaged Solr distribution with: bundle exec rake sunspot:solr:start # or sunspot:solr:run to start in foreground

Setting Up Objects
Add a  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 fields will be full-text searchable. Other fields (e.g.,  and  ) can be used to scope queries.

Searching Objects
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

Search In Depth
Given an object  setup in earlier steps ...

Full Text
Post.search { fulltext 'pizza' } Post.search do fulltext 'pizza' do    boost_fields :title => 2.0 end end Post.search do fulltext 'pizza' do    boost(2.0) { with(:featured, true) } end end Post.search do fulltext 'pizza' do    fields(:title) end end Post.search do fulltext 'pizza' do    fields(:body, :title => 2.0) end end
 * 1) All posts with a `text` field (:title, :body, or :comments) containing 'pizza'
 * 1) Posts with pizza, scored higher if pizza appears in the title
 * 1) Posts with pizza, scored higher if featured
 * 1) Posts with pizza *only* in the title
 * 1) Posts with pizza in the title (boosted) or in the body (not boosted)

sunspot
Install the required gem dependencies: cd /path/to/sunspot/sunspot bundle install Start a Solr instance on port 8983: bundle exec sunspot-solr start -p 8983 Run the tests: bundle exec rake spec If desired, stop the Solr instance: bundle exec sunspot-solr stop
 * 1) or `bundle exec sunspot-solr run -p 8983` to run in foreground

sunspot_rails
Install the gem dependencies for : cd /path/to/sunspot/sunspot bundle install Start a Solr instance on port 8983: bundle exec sunspot-solr start -p 8983 Navigate to the  directory: cd ../sunspot_rails Run the tests: rake spec # all Rails versions rake spec RAILS=3.1.1 # specific Rails version only If desired, stop the Solr instance: cd ../sunspot bundle exec sunspot-solr stop
 * 1) or `bundle exec sunspot-solr run -p 8983` to run in foreground

Generating Documentation
Install the  and   gems: $ gem install yard redcarpet Uninstall the  gem, if installed: $ gem uninstall rdiscount Generate the documentation from topmost directory: $ yardoc -o docs */lib/**/*.rb - README.md