introduction to elasticsearch

23
ElasticSearch Presented by: Abuzar Hasan Arkhitech <http://www.arkhitech.com>

Upload: arkhitech

Post on 08-May-2015

749 views

Category:

Technology


0 download

DESCRIPTION

Introduction to ElasticSearch and using Tire

TRANSCRIPT

Page 1: Introduction to ElasticSearch

ElasticSearch

Presented by: Abuzar HasanArkhitech <http://www.arkhitech.com>

Page 2: Introduction to ElasticSearch

History

Shay Baron Compass in 2004 ElasticSearch in 2010

Written in Java Cross-platform Distributed

Page 3: Introduction to ElasticSearch

Users StumbleUpon

Mozilla

Github

Foursquare

Page 4: Introduction to ElasticSearch

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

Page 5: Introduction to ElasticSearch

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

Page 6: Introduction to ElasticSearch

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

Page 7: Introduction to ElasticSearch

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

Page 8: Introduction to ElasticSearch

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'

Page 9: Introduction to ElasticSearch

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>].

Page 10: Introduction to ElasticSearch

Indexing Creating an index curl -XPUT "http://localhost:9200/movies/movie/1" -d'

{ "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972 }'

Page 11: Introduction to ElasticSearch

Indexing Response After executing the request, we get the following response

{ “ok”: true, “_index”: “movies”, “_type”: “movie”, “_id”: “1”, “_version”: 1

}

Page 12: Introduction to ElasticSearch

Retrieving Index Retrieving the index

curl -XGET "http://localhost:9200/movies/movie/1" -d''

Page 13: Introduction to ElasticSearch

Retrieve Index Response {

“_index”: “movies”, “_type”: “movie”, “_id”: “1”, “_version”: 1, “exists”: true, “_source”: {

"title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972

} }

Page 14: Introduction to ElasticSearch

Delete Index Deleting the index

curl -XDELETE "http://localhost:9200/movies/movie/1" -d''

Page 15: Introduction to ElasticSearch

Demo Application Demo Application by karmi

$ rails new searchapp -m https://raw.github.com/karmi/tire/master/exam les/rails-application-template.rb

Page 16: Introduction to ElasticSearch

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

Page 17: Introduction to ElasticSearch

Demo: Search App - Controller

Controller @articles = Article.tire.search params[:query]

This line in your search method helps to search using tire gem

Page 18: Introduction to ElasticSearch

Demo: Search App - View View

<%= form_tag search_articles_path, method: :get do %> <%= text_field_tag :q, params[:query] %> <%= submit_tag :search %> <% end %>

Page 19: Introduction to ElasticSearch

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

Page 20: Introduction to ElasticSearch

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

Page 21: Introduction to ElasticSearch

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

Page 22: Introduction to ElasticSearch

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

Page 23: Introduction to ElasticSearch

QUESTIONS?

References• https://github.com/karmi/retire• http://railscasts.com/episodes/306-elasticsearch-part-

1