CSC/ECE 517 Spring 2015/ch1b 18 AS: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 12: Line 12:


Sunspot makes it easy to do full text searching through Solr. Sunspot comes as a gem and is installed in the usual way by adding it to the Gemfile and running bundle.
Sunspot makes it easy to do full text searching through Solr. Sunspot comes as a gem and is installed in the usual way by adding it to the Gemfile and running bundle.
<div class="code_block">
        <div class="code_header">
          /Gemfile
        </div>
        <div class="CodeRay">
  <div class="code"><pre>source <span class="s"><span class="dl">'</span><span class="k">http://rubygems.org</span><span class="dl">'</span></span>
gem <span class="s"><span class="dl">'</span><span class="k">rails</span><span class="dl">'</span></span>, <span class="s"><span class="dl">'</span><span class="k">3.0.9</span><span class="dl">'</span></span>
gem <span class="s"><span class="dl">'</span><span class="k">sqlite3</span><span class="dl">'</span></span>
gem <span class="s"><span class="dl">'</span><span class="k">nifty-generators</span><span class="dl">'</span></span>
gem <span class="s"><span class="dl">'</span><span class="k">sunspot_rails</span><span class="dl">'</span></span></pre></div>
</div>
      </div>


<pre>
<pre>
gem 'sunspot_rails'
gem 'sunspot_rails'
gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development
</pre>
</pre>


You could install pry on Rails by adding the pry-rails gem to your gem file:
Bundle it.


<pre>
<pre>
group :development do
bundle install
  gem 'pry'
  ...
end
</pre>
</pre>


Once you have got that settled, change back into the sample_app directory and install all the necessary dependencies:
Once the gem and its dependencies have installed we will need to generate Sunspot’s configuration file which we can do by running


<pre>
<pre>
bundle install
$ rails g sunspot_rails:install
</pre>
</pre>


Setting up the database:
This command creates a YML file at /config/sunspot.yml. We don’t need to make any changes to the default settings in this file.
Sunspot embeds Solr inside the gem so there’s no need to install it separately. This means that it works straight out of the box which makes it far more convenient to use in development. To get it up and running we run


<pre>
<pre>
rake db:schema:load
$ rake sunspot:solr:start
</pre>
</pre>


Note that some of you might need to prefix the above command with bundle exec.
If you’re running OS X Lion and you haven’t installed a Java runtime you’ll be prompted to do so when you run this command. You may also see a deprecation warning but this can be safely ignored. The command will also create some more configuration files for advanced configuration.


One final thing before we proceed to the actual stuff: Create .pryrc in your home directory and fill it in with the editor of your choice:
=== Indexing Objects ===
Add a searchable block to the objects you wish to index.


<pre>
<pre>
% touch ~/.pryrc
class Example < ActiveRecord::Base
Pry.config.editor = 'vim'
  searchable do
    text :title, :body
    text :comments do
      comments.map { |comment| comment.body }
    end
 
    integer :blog_id
    integer :author_id
    string  :sort_title do
      title.downcase.gsub(/^(an?|the)/, '')
    end
  end
end
</pre>
</pre>


To launch pry through the terminal you simply write:
Text fields will be full-text searchable. Other fields (e.g., integer and string) can be used to scope queries.
 
=== Searching Objects ===
Now searching can be done simply by passing the query to search method in the respective class.


<pre>
<pre>
rails c
Example.search do
  fulltext 'best pizza'
  with :blog_id, 1
  facet :author_id
end
</pre>
</pre>
We can use many variations on the search now by changing the scope variables in the query.


and you’re greeted with a pry console:


<pre>
% rails c
Loading development environment (Rails 3.2.16)
Frame number: 0/3
[1] sample_app »
</pre>
=='''References'''==
=='''References'''==
<references/>
<references/>

Revision as of 10:49, 17 February 2015

Apache Solr and Rails


Apache Solr is a standalone enterprise search server with a REST-like API. Indexing could be done using JSON, XML, CSV or binary over Hyper text transfer protocol. It could be then queried using HTTP with a GET method and receive the JSON, XML, CSV or binary results. It is a popular, scalable, blazing-fast, open source enterprise search platform built on <ref>http://lucene.apache.org/index.html</ref>Apache Lucene search library. Websites using Rails can take advantage of the Solr search engine to provide sophisticated and customizable search features. Ruby/Rails integrates with Solr search server using Sunspot (sunspot, sunspot_rails gem) library, to do a full-text search.

Introduction

Apache Solr is a standalone enterprise search server with a REST-like API. Indexing could be done using JSON, XML, CSV or binary over Hyper text transfer protocol. It could be then queried using HTTP with a GET method and receive the JSON, XML, CSV or binary results. It is a popular, scalable, blazing-fast, open source enterprise search platform built on <ref>http://lucene.apache.org/index.html</ref>Apache Lucene. Websites using rails can take advantage of the Solr search engine to provide sophisticated and customizable search features.

Installation

Sunspot makes it easy to do full text searching through Solr. Sunspot comes as a gem and is installed in the usual way by adding it to the Gemfile and running bundle.

gem 'sunspot_rails'
gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development

Bundle it.

bundle install

Once the gem and its dependencies have installed we will need to generate Sunspot’s configuration file which we can do by running

$ rails g sunspot_rails:install

This command creates a YML file at /config/sunspot.yml. We don’t need to make any changes to the default settings in this file. Sunspot embeds Solr inside the gem so there’s no need to install it separately. This means that it works straight out of the box which makes it far more convenient to use in development. To get it up and running we run

$ rake sunspot:solr:start

If you’re running OS X Lion and you haven’t installed a Java runtime you’ll be prompted to do so when you run this command. You may also see a deprecation warning but this can be safely ignored. The command will also create some more configuration files for advanced configuration.

Indexing Objects

Add a searchable block to the objects you wish to index.

class Example < ActiveRecord::Base
  searchable do
    text :title, :body
    text :comments do
      comments.map { |comment| comment.body }
    end

    integer :blog_id
    integer :author_id
    string  :sort_title do
      title.downcase.gsub(/^(an?|the)/, '')
    end
  end
end

Text fields will be full-text searchable. Other fields (e.g., integer and string) can be used to scope queries.

Searching Objects

Now searching can be done simply by passing the query to search method in the respective class.

Example.search do
  fulltext 'best pizza'
  with :blog_id, 1
  facet :author_id
end
We can use many variations on the search now by changing the scope variables in the query.


References

<references/>