Introduction to ElasticSearch

1,043 views

Published on

Introduction to ElasticSearch and using Tire

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,043
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to ElasticSearch

  1. 1. ElasticSearch  Presented by: Abuzar Hasan Arkhitech <http://www.arkhitech.com>
  2. 2. History  Shay Baron   Compass in 2004 ElasticSearch in 2010    Written in Java Cross-platform Distributed
  3. 3. Users     StumbleUpon Mozilla Github Foursquare
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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/elasticsear ch.yml
  8. 8. 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'
  9. 9. 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>].
  10. 10. Indexing   Creating an index curl -XPUT "http://localhost:9200/movies/movie/1" -d' {  "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972     }'
  11. 11. Indexing Response  After executing the request, we get the following response {        } “ok”: “_index”: “_type”: “_id”: “_version”: true, “movies”, “movie”, “1”, 1
  12. 12. Retrieving Index  Retrieving the index  curl -XGET "http://localhost:9200/movies/movie/1" -d''
  13. 13. Retrieve Index Response {        “_index”: “_type”: “_id”: “_version”: “exists”: “_source”:      } } “movies”, “movie”, “1”, 1, true, { "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972
  14. 14. Delete Index  Deleting the index  curl -XDELETE "http://localhost:9200/movies/movie/1" -d''
  15. 15. Demo Application  Demo Application by karmi  $ rails new searchapp -m https://raw.github.com/karmi/tire/master/exam les/railsapplication-template.rb
  16. 16. 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
  17. 17. Demo: Search App - Controller  Controller   @articles = Article.tire.search params[:query] This line in your search method helps to search using tire gem
  18. 18. Demo: Search App - View  View     <%= form_tag search_articles_path, method: :get do %> <%= text_field_tag :q, params[:query] %> <%= submit_tag :search %> <% end %>
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. Displaying Facets  For displaying facets s.results.facets['global-tags']['terms'].each do |f|     puts "#{f['term'].ljust(10)} #{f['count']}" end Output     ruby python php java 3 1 1 1
  23. 23. References • https://github.com/karmi/retire • http://railscasts.com/episodes/306-elasticsearch-part1 QUESTIONS?

×