ElasticSearch with Rails

Apache Lucene is a very powerful library to search text with more efficiently. Lucene can make index and search all the data that can be converted to a format textual.Lucene ignoring data source, format, or even the language that is used for can be converted to text. Lucene can be used to index and search data stored in file: web page on a remote web server, the documents stored on the local file system, text files, Microsoft Word documents, HTML or PDF file, or all of the formats that can be converted into textual information.

 

2. How to Install ElasticSearch

 

Linux Ubuntu


$ sudo apt-get install openjdk-7-jre-headless -y ( 40Mb )
	

https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.0.deb ( 16Mb )
	

$ sudo dpkg -i elasticsearch-0.90.0.deb
	

$ sudo service elasticsearch start
	

3. ElasticSearch in Heroku


if you want to activate elasticsearch in heroku, you can follow the instruction in link below devcenter.heroku.com/articles/bonsai

 

Creating Sample Rails Application


  • rails new elasticsample

  • add gem "tire", :git => "git://github.com/karmi/tire.git" into your gemfile and bundle install

  • create scaffold rails g scaffold Articles title:string tags:string content:text published_on:date

  • rake db:create; rake db:migrate

  • add search from in views/articles/index.html.erb


[code language="ruby"]
  // add search form in views/articles/index.html.erb
  <%= form_tag articles_path, method: :get do %> <%= text_field_tag :keyword, params
  [:keyword] %> <%= submit_tag "Search", name: nil %> <% end %>
[/code] 

[code language="ruby"] 
 // include Tire to model Article for connect to elasticsearch service include Tire::Model::Search include Tire::Model::Callbacks
	

 

// and then create method

def self.search(keyword) tire.search do query { string keyword } end end [/code] don't forget to storing them to index : $ rake environment tire:import CLASS='Article'

[code language="ruby"] 
 // change your index article controller if params[:keyword].present? @articles = Article.search(params[:keyword]) else @articles = Article.all end 
[/code]
	

 

Run application and add 3 data or more and try to search some data

You can do more with ElasticSearch and Tire


  • Mapping

    process of defining how a document should be mapped to the Search Engine, including its searchable characteristics such as which fields are searchable and if/how they are tokenized

  • Faceted Search

    allows users to explore a collection of information by successively applying filters in whatever order they chooses.

  • Geo Location and Search

  • etc.


4. Mapping

 

Example how to use mapping

[code language="ruby"] // add in article model

mapping do indexes :id, :index => :not_analyzed indexes :title, :analyzer => 'snowball', :boost => 100 indexes :content, :analyzer => 'snowball' indexes :content_size, :as => 'content.size' indexes :tags, :analyzer => 'snowball' indexes   :published_on, :type => 'date', :include_in_all => false 
 // you can remove :include_in_all => false,
 // if you want to allow search text by published_on end
[/code] 

You can open elasticsearch reference guide at : http://www.elasticsearch.org/guide/ , for more information about indexes, type, analyzer, and about boost, etc.

 

5. Faceted Search

 

Example how to use Faceted Search

[code language="ruby"]
  def self.search(keyword) tire.search do query do string keyword end
  filter :terms, :tags => ['action'] // allow search only for tags = action sort { by :title, 'desc' } // sort result by result
  acet 'global-tags', :global => true do terms :tags end // using facet with block
  facet('timeline') { date :published_on, :interval => 'month' } // using facet without block and set facet by interval
  end end 
[/code]
 	

 

6. Geo Location Search


Example how to use Faceted Search

[code language="ruby"]
 def self.search(keyword) tire.search do query do string keyword end filter :geo_distance, lat_lon: "41,-71", distance: '50km' end end 
[/code]
	


Ruby On Rails Blog and Web Development | 41studio

By Admin on September 19, 2013