mastering elasticsearch with ruby and tire

42
Mastering ElasticSearch w/ Ruby (+ Tire) RubyConf 2013 Luca Bonmassar, Gild Tuesday, 5 November 13

Upload: luca-bonmassar

Post on 26-Jan-2015

108 views

Category:

Technology


2 download

DESCRIPTION

A tutorial on what is ElasticSearch and how to use it effectively in a real project. The talk discusses how to integrate a search experience in an existing application, showing all the steps from downloading&configuring elastic search, to building the UI and wire the search logic (in a Rails application). The talk was presented at RubyConf 2013.

TRANSCRIPT

Page 1: Mastering ElasticSearch with Ruby and Tire

MasteringElasticSearch w/ Ruby (+ Tire)

RubyConf 2013

Luca Bonmassar, Gild

Tuesday, 5 November 13

Page 2: Mastering ElasticSearch with Ruby and Tire

Who am I?I’m Luca Bonmassar (@openmosix) # 31 # Italian living in San Francisco (and Stockholm)

I work at Gild

I love building products [for fun, profit and boredom]

Tuesday, 5 November 13

Page 3: Mastering ElasticSearch with Ruby and Tire

Search - use case You’re building a product

User generated content

Let (other) users find or discover this content

Tuesday, 5 November 13

Page 4: Mastering ElasticSearch with Ruby and Tire

Search is NOT easy It usually starts as

but then you want to support AND, OR, NOT, double quotes on multiple fields so

and then it goes like

Tuesday, 5 November 13

Page 5: Mastering ElasticSearch with Ruby and Tire

And so it goes...

Tuesday, 5 November 13

Page 6: Mastering ElasticSearch with Ruby and Tire

AgendaLet’s define a “pet project”

Boilerplate (download, install, scaffold, config, bla bla bla, yadda, yadda, yadda)

Build a website w/ simple search

Build a more advanced search

What next (homework)

Tuesday, 5 November 13

Page 7: Mastering ElasticSearch with Ruby and Tire

Project! (for fun and no profit)

Tuesday, 5 November 13

Page 8: Mastering ElasticSearch with Ruby and Tire

Project: build a full text-search for RubyGems.org

Tuesday, 5 November 13

Page 9: Mastering ElasticSearch with Ruby and Tire

RubyGems Search Architecture

RubyGems.org

MongoDB

Rails App (Tire)

Elastic Search

Web Spider

Tuesday, 5 November 13

Page 10: Mastering ElasticSearch with Ruby and Tire

RubyGems crawler

github.com/openmosix/rubygems-crawler

1. Download all ruby gem names from https://rubygems.org/gems2. Use gems API to download each gem info: read JSON - write JSON (MongoDB)

Tuesday, 5 November 13

Page 11: Mastering ElasticSearch with Ruby and Tire

RubyGems Crawler II

Tuesday, 5 November 13

Page 12: Mastering ElasticSearch with Ruby and Tire

RubyGems Search v1

Code: github.com/openmosix/rubygems-searchSpoiler: http://rubyconf.bonmassar.it

Tuesday, 5 November 13

Page 13: Mastering ElasticSearch with Ruby and Tire

ElasticSearch

Tuesday, 5 November 13

Page 14: Mastering ElasticSearch with Ruby and Tire

ElasticSearch II Open source search+analytics engine

Distributed[near] Realtime searchMulti tenantBuilt on Apache Lucene

REST APIsJSON documents

Tuesday, 5 November 13

Page 15: Mastering ElasticSearch with Ruby and Tire

1. Download / set up a ES cluster

4. Query Index3. Index Data2. Define settings and data mapping (opt)

ElasticSearch III

Elastic Search MongoDB

Curl>

curl  -­‐X  GET  'h.p://localhost:9200/ruby_gems/_search?from=0&size=25&pre.y'

{    "ok"  :  true,    "status"  :  200,    "version"  :  {        "number"  :  "0.90.5",        "lucene_version"  :  "4.4"    },    "tagline"  :  "You  Know,  for  Search"}

Tuesday, 5 November 13

Page 17: Mastering ElasticSearch with Ruby and Tire

ES config > ls elasticsearch-0.90.6/config/

Logging.yml: where to log, how much to logElasticsearch.yml: all server config. Defines:Name of the cluster (change it!!!)Node parameters (master/slave, store data/router)Sharing and # replicasPathsPluginsMemory (JVM, heap, memory locking)Network config“Gateway” (cluster backup)RecoveryDiscoverySlow log + GC log

Default options are good enough for dev env

Tuesday, 5 November 13

Page 18: Mastering ElasticSearch with Ruby and Tire

ES boot + test Start: bin/elasticsearch

Test: curl http://localhost:9200/{ "ok" : true, "status" : 200, "name" : "Iron Fist", "version" : { "number" : "0.90.6", "lucene_version" : "4.5.1" }, "tagline" : "You Know, for Search"}

Stop: curl -XPOST 'http://localhost:9200/_shutdown'

{"cluster_name":"elasticsearch","nodes":{"jm2Z3J4dSzebjJ7Px2fAJg":{"name":"Iron Fist"}}}

Tuesday, 5 November 13

Page 19: Mastering ElasticSearch with Ruby and Tire

Profit(you are now an ElasticSearch expert - go and tell the world)

Tuesday, 5 November 13

Page 20: Mastering ElasticSearch with Ruby and Tire

ElasticSearch operations

Create a “RubyGem” IndexDefines a “RubyGem” Index data mapping

Index data (e.g. upload data from MongoDB to ES index = POST)

Query (= GET)

Tuesday, 5 November 13

Page 21: Mastering ElasticSearch with Ruby and Tire

Tire now Re-Tire ;(

A ruby gem wrapping ElasticSearch REST APIs into a powerful ruby DSL

ActiveModel integration

Rake tasks and utilities to load and query ElasticSearch

Tuesday, 5 November 13

Page 22: Mastering ElasticSearch with Ruby and Tire

Tire setupcat “gem ‘tire’” > Gemfile && bundle install

> cat config/initializers/elasticsearch.rb...Tire::Configuration.url('http://localhost:9200')Tire.configure { logger "#{Rails.root}/log/elasticsearch-queries.log" } if ENV['ES_LOG']

Tuesday, 5 November 13

Page 23: Mastering ElasticSearch with Ruby and Tire

Define an ES index (with Tire DSL)

Tuesday, 5 November 13

Page 24: Mastering ElasticSearch with Ruby and Tire

Indexing Get a recordConvert it to JSON format (to_indexed_json)Push it to Elastic Search (.update_index)

...under the hood...

Tuesday, 5 November 13

Page 25: Mastering ElasticSearch with Ruby and Tire

Index (all data) Naive (POST on index for each record):

Use bulk updates:

...under the hood...

Tuesday, 5 November 13

Page 26: Mastering ElasticSearch with Ruby and Tire

Search I

Tuesday, 5 November 13

Page 27: Mastering ElasticSearch with Ruby and Tire

Search II

Tuesday, 5 November 13

Page 28: Mastering ElasticSearch with Ruby and Tire

Simple Search

Tuesday, 5 November 13

Page 29: Mastering ElasticSearch with Ruby and Tire

Highlight matches

Text

...add some CSS...

Tuesday, 5 November 13

Page 30: Mastering ElasticSearch with Ruby and Tire

Advanced Search

Tuesday, 5 November 13

Page 31: Mastering ElasticSearch with Ruby and Tire

Advanced Search II

Tuesday, 5 November 13

Page 32: Mastering ElasticSearch with Ruby and Tire

Advanced Search III

Tuesday, 5 November 13

Page 33: Mastering ElasticSearch with Ruby and Tire

Facets

Tuesday, 5 November 13

Page 34: Mastering ElasticSearch with Ruby and Tire

ES facets

Tuesday, 5 November 13

Page 35: Mastering ElasticSearch with Ruby and Tire

ES facets (running)

Tuesday, 5 November 13

Page 36: Mastering ElasticSearch with Ruby and Tire

Facets - UI

Tuesday, 5 November 13

Page 37: Mastering ElasticSearch with Ruby and Tire

Bonsai CoolI Search Suggesters (Did you mean... ?)

Tuesday, 5 November 13

Page 38: Mastering ElasticSearch with Ruby and Tire

Bonsai Cool II

“Similar to this” (aka “More Like This” API)

Tuesday, 5 November 13

Page 39: Mastering ElasticSearch with Ruby and Tire

Bonsai Cool III

Percolate API

Tuesday, 5 November 13

Page 40: Mastering ElasticSearch with Ruby and Tire

DeploymentI Run your own cluster

Some learnings: at least 3 nodesmemory profiling / GCinstall very good monitoring (github.com/karmi/elasticsearch-paramedic)more RAM is (always) betterCheck IOPS (if on AWS)

Pros:Total controlCheaper (lot cheaper)

Cons:Can be a nightmare / Require dedicated devop

Tuesday, 5 November 13

Page 41: Mastering ElasticSearch with Ruby and Tire

Deployment II ElasticSearch as a service

http://found.nohttp://searchly.comhttp://bonsai.io

Pros:Get cluster up & running in a minuteFocus on dev, not troubleshootingProfessional support

Cons:ExpensiveCan be in the wrong region / hosting providerExpensiveDid I say expensive?

Tuesday, 5 November 13

Page 42: Mastering ElasticSearch with Ruby and Tire

Thanks!Code: github.com/openmosix/rubygems-searchgithub.com/openmosix/rubygems-crawler

Demo (will be down by the end of rubyconf): http://rubyconf.bonmassar.it

Say “hi”:Luca Bonmassar - [email protected]

twitter.com/openmosix

github.com/openmosix

linkedin.com/in/lucabonmassar

Tuesday, 5 November 13