Sunspot provides an interface between an application (usually Ruby on Rails) and a Solr index. This interface allows the application to send and query data very easily, using Solr as the search engine. A few lines of code are all that is needed to get a basic search function set up.

Solr (pronounced “solar”) is an open-source enterprise-search platform, written in Java, from the Apache Lucene project. It uses the Lucene Java search library at its core for full-text indexing and search, and has REST-like HTTP/XML and JSON APIs that make it usable from most popular programming languages.

How  does solr engine works?

Apache Solr is a search engine. you index a set of document (say, news articles) and then query Solr to return a set of documents that matches user query. So, for instance,  You run a code to read each article and then add the required information to the Solr Index (the inverted index, of course).

Solr is a structured-document database which uses index and searches for technologies to perform its features, some of them are full-text-search, faceted search, real-time indexing, etc.

Installing dependencies for Solr

Solr requires Java. For this fresh installation of Ubuntu, the package manager apt-get install a Java runtime environment and the curl utility, which is useful for interacting with web services from the command line.

$ sudo apt-get update$ sudo apt-get install openjdk-6-jre-headless curl

Downloading, unpacking, and running Solr

Solr is available for download from one of Apache’s mirrors listed on the download page.

$ wget http://apache.osuosl.org/lucene/solr/1.4.0/apache-solr-1.4.0.tgz$ tar -xvzf apache-solr-1.4.0.tgz

After unpacking, Solr’s example package demonstrates whether everything is working.

$ cd apache-solr-1.4.0/example$ java -jar start.jar # generates a lot of output.

Sunspot

Sunspot is a Ruby library for expressive, powerful interaction with the Solr search engine. Sunspot is built on top of the RSolr library, 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 for them.

Sunspot is designed to be easily plugged into any ORM, or even non-database-backed objects such as the filesystem.

Quickstart with Rails

1. Add to Gemfile:

gem ‘sunspot_rails’

gem ‘sunspot_solr’ # optional pre-packaged Solr distribution for use in development. Not for use in production.

2. Bundle it:

bundle install

Generate a default configuration file:

rails generate sunspot_rails:install

3. 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.This will generate a /solr folder with default configuration files and indexes.

4. If you’re using source control, it’s recommended that the files generated for indexing and running (PIDs) are not checked in.