Date post: | 08-May-2015 |
Category: |
Technology |
Upload: | arkhitech |
View: | 729 times |
Download: | 0 times |
ElasticSearch
Presented by: Abuzar HasanArkhitech <http://www.arkhitech.com>
History
Shay Baron Compass in 2004 ElasticSearch in 2010
Written in Java Cross-platform Distributed
Users StumbleUpon
Mozilla
Github
Foursquare
Features Distributed and Highly Available Search Engine
Each index is fully sharded with a configurable number of shards Each shard can have one or more replicas Read / Search operations performed on either one of the replica
shard Multi Tenant with Multi Types Various set of APIs
Features Document Oriented
No need for upfront schema definition (Near) Real Time Search Per-Operation Persistence
Single document level operations are atomic, consistent, isolated and durable
Features Built on top of Lucene
Each shard is a fully functional Lucene index All the power of Lucene easily exposed through simple
configuration / plugins Independent of file format
Open Source under Apache 2 License
Setting Up Elastic Search Download ElasticSearch (CentOS)
sudo yum install elasticsearch
Start ElasticSearch Server elasticsearch -f -D
es.config=/usr/local/Cellar/elasticsearch/0.18.5/config/elasticsearch.yml
Setting up ElasticSearch w/ Ruby
The ElasticSearch is not ruby specific so 'tire' gem helps ruby based projects to communicate with ElasticSearch
'tire' gem https://github.com/karmi/retire
We then need to require following libraries require 'rubygems' require 'tire'
Indexing Records Indexing includes both “Create” and “Update” in CRUD. In order to make index request for new JSON object, we pass
the following URL http://localhost:<port>/<index>/<type>/[<id>].
Indexing Creating an index curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{ "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972 }'
Indexing Response After executing the request, we get the following response
{ “ok”: true, “_index”: “movies”, “_type”: “movie”, “_id”: “1”, “_version”: 1
}
Retrieving Index Retrieving the index
curl -XGET "http://localhost:9200/movies/movie/1" -d''
Retrieve Index Response {
“_index”: “movies”, “_type”: “movie”, “_id”: “1”, “_version”: 1, “exists”: true, “_source”: {
"title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972
} }
Delete Index Deleting the index
curl -XDELETE "http://localhost:9200/movies/movie/1" -d''
Demo Application Demo Application by karmi
$ rails new searchapp -m https://raw.github.com/karmi/tire/master/exam les/rails-application-template.rb
Demo: Search App - Model
Model include Tire::Model::Search include Tire::Model::Callbacks
These two 'tire' models allow your rails application to use tire gem for searching purposes
Demo: Search App - Controller
Controller @articles = Article.tire.search params[:query]
This line in your search method helps to search using tire gem
Demo: Search App - View View
<%= form_tag search_articles_path, method: :get do %> <%= text_field_tag :q, params[:query] %> <%= submit_tag :search %> <% end %>
Demo: Search App - Index Create an index
Tire.index 'articles' do delete create store :title => 'One', :tags => ['ruby'] store :title => 'Two', :tags => ['ruby', 'python'] store :title => 'Three', :tags => ['java'] store :title => 'Four', :tags => ['ruby', 'php'] refresh end
Demo: Search App - Index For custom mapping
Tire.index 'articles' do delete create :mappings => { :article => { :properties => { :id => { :type => 'string', :index => 'not_analyzed', :include_in_all => false }, :title => { :type => 'string', :boost => 2.0, :analyzer => 'snowball' }, :tags => { :type => 'string', :analyzer => 'keyword' }, :content => { :type => 'string', :analyzer => 'snowball' }
} } } end
Demo: Search App – Bulk Indexing
For indexing large amount of data articles = [
{ :id => '1', :type => 'article', :title => 'one', :tags => ['ruby'] }, { :id => '2', :type => 'article', :title => 'two', :tags => ['ruby', 'python'] }, { :id => '3', :type => 'article', :title => 'three', :tags => ['java'] }, { :id => '4', :type => 'article', :title => 'four', :tags => ['ruby', 'php'] }
] Use Import
Tire.index 'articles' do import articles end
Displaying Facets For displaying facets
s.results.facets['global-tags']['terms'].each do |f| puts "#{f['term'].ljust(10)} #{f['count']}"
end
Output ruby 3 python 1 php 1 java 1
QUESTIONS?
References• https://github.com/karmi/retire• http://railscasts.com/episodes/306-elasticsearch-part-
1