SlideShare a Scribd company logo
Copyright © 2014 Intridea Inc. All rights reserved.
Elasticsearch with Rails
July 10, 2014
Tom Zeng
Director of Engineering
tom@intridea.com
@tomzeng
www.linkedin.com/in/tomzeng
What is Elasticsearch
!
Elasticsearch is a “flexible and powerful open source, distributed real-time
search and analytics engine for the cloud”
More than just full text search, it has powerful analytics capability, and can
be used as a NoSQL data store
Easy to setup, easy to use, easy to scale, easy to maintain
Suitable for projects of any size (large and small, cloud or non-cloud) that
need full text search and/or analytics, it’s our preferred search engine for
Rails apps
Elasticsearch Quick Live Demo
Use curl to add some data (local Elasticsearch instance at port 9200)
!
curl -X DELETE "http://localhost:9200/todos" (clean up the index and start from scratch)
!
curl -X POST "http://localhost:9200/todos/task/1" -d '{"title" : "Learn Elasticsearch",
"due_date" : "20140710T00:00:00", "done" : true, "tags" : ["seach","backend"]}'
!
curl -X POST "http://localhost:9200/todos/task/2" -d '{"title" : "Learn D3 and Backbone",
"due_date" : "20140720T00:00:00", "done" : true, "tags" :
["frontend","javascript","visualization"]}'
!
curl -X POST "http://localhost:9200/todos/task/3" -d '{"title" : "Learn Rails 4",
"due_date" : "20140830T00:00:00", "done" : false, "tags" : ["backend","ruby","rails"]}'
!
curl -X POST "http://localhost:9200/todos/task/4" -d '{"title" : "Learn Backbone
Marionette", "due_date" : "20140715T00:00:00", "done" : true, "tags" :
["frontend","javascript"]}'
!
Elasticsearch Quick Live Demo
Use curl to query data
curl http://localhost:9200/todos/task/1 (use as K/V store)
curl http://localhost:9200/todos/_search?pretty&q=done:false
curl http://localhost:9200/todos/_search?pretty&q=tags:backend
curl http://localhost:9200/todos/_search?pretty&q=title:back*
curl -X POST "http://localhost:9200/todos/task/_search?pretty" -d '
{
query : {
range : { due_date : { from : "20140701", to : "20140715" } }
}
}'
!
Elasticsearch Quick Live Demo
What is Elasticsearch
http://www.elasticsearch.org
What is Elasticsearch
http://www.elasticsearch.org
What is Elasticsearch
http://www.elasticsearch.org
Who uses Elasticsearch
http://www.elasticsearch.org
Who uses Elasticsearch - Github
Sort
HighlightFacets
Filters
Fulltext Search
Pagination
Elasticsearch Key Concepts
Cluster – A cluster consists of one or more nodes which share the same cluster name. Each
cluster has a single master node which is chosen automatically by the cluster and which can be
replaced if the current master node fails.
Node – A node is a running instance of elasticsearch which belongs to a cluster. Multiple
nodes can be started on a single server for testing purposes, but usually you should have one
node per server.
At startup, a node will use unicast (or multicast, if specified) to discover an existing cluster with
the same cluster name and will try to join that cluster.
Index – An index is like a ‘database’ in a relational database. It has a mapping which
defines multiple types.
An index is a logical namespace which maps to one or more primary shards and can have zero
or more replica shards.
Type – A type is like a ‘table’ in a relational database. Each type has a list of fields that can be
specified for documents of that type. The mapping defines how each field in the document is
analyzed. http://www.elasticsearch.org/guide/reference/glossary
Elasticsearch Key Concepts
Document – A document is a JSON document which is stored in elasticsearch. It is like a
row in a table in a relational database. Each document is stored in an index and has a type and
an id.
A document is a JSON object (also known in other languages as a hash / hashmap / associative
array) which contains zero or more fields, or key-value pairs. The original JSON document that is
indexed will be stored in the _source field, which is returned by default when getting or
searching for a document.
Field – A document contains a list of fields, or key-value pairs. The value can be a simple
(scalar) value (eg a string, integer, date), or a nested structure like an array or an object. A field
is similar to a column in a table in a relational database.
The mapping for each field has a field ‘type’ (not to be confused with document type) which
indicates the type of data that can be stored in that field, eg integer, string, object. The mapping
also allows you to define (amongst other things) how the value for a field should be analyzed.
Mapping – A mapping is like a ‘schema definition’ in a relational database. Each index has
a mapping, which defines each type within the index, plus a number of index-wide settings. A
mapping can either be defined explicitly, or it will be generated automatically when a document
is indexed http://www.elasticsearch.org/guide/reference/glossary
Elasticsearch Key Concepts
Shard – A shard is a single Lucene instance. It is a low-level “worker” unit which is managed
automatically by elasticsearch. An index is a logical namespace which points to primary and
replica shards.
Elasticsearch distributes shards amongst all nodes in the cluster, and can move shards
automatically from one node to another in the case of node failure, or the addition of new
nodes.
Primary Shard – Each document is stored in a single primary shard. When you index a
document, it is indexed first on the primary shard, then on all replicas of the primary shard. By
default, an index has 5 primary shards. You can specify fewer or more primary shards to scale
the number of documents that your index can handle.
Replica Shard – Each primary shard can have zero or more replicas. A replica is a copy of
the primary shard, and has two purposes: 1) increase failover: a replica shard can be promoted
to a primary shard if the primary fails. 2) increase performance: get and search requests can be
handled by primary or replica shards.
!
!
http://www.elasticsearch.org/guide/reference/glossary
Elasticsearch Key Concepts
!
Elasticsearch SQL
!
Index => Database
Type => Table
Document => Row
Field => Column
Mapping => Schema
Shard => Partition
!
!
Elasticsearch Installation
OS X
brew install elasticsearch
Ubuntu
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.deb
sudo dpkg -i elasticsearch-1.1.1.deb
Centos
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.noarch.rpm
sudo yum install elasticsearch-1.1.1.noarch.rpm
!
Elasticsearch Status Check
Elasticsearch Cluster Status
Elasticsearch Monitoring
elasticsearch-head - https://github.com/mobz/elasticsearch-head
!
!
!
!
Marvel - http://www.elasticsearch.org/guide/en/marvel/current/#_marvel_8217_s_dashboards
Paramedic - https://github.com/karmi/elasticsearch-paramedic
Bigdesk - https://github.com/lukas-vlcek/bigdesk/
!
Elasticsearch APIs
Elasticsearch API Examples
Use curl to run the query and facet APIs
curl -X POST "http://localhost:9200/todos/_search?pretty=true" -d '
{
"query" : { "query_string" : {"query" : "Learn*"} },
"facets" : {
"tags" : { "terms" : {"field" : "tags"} }
}
}
'
Facets – todos tagged with keywords
javascript: 2
frontend: 2
backend: 2
visualization: 1
!
!
Elasticsearch Query DSL
http://www.elasticsearch.org
Elasticsearch Query DSL Examples
http://www.elasticsearch.org
Elasticsearch Query DSL Examples
Elasticsearch Query DSL Examples
http://www.elasticsearch.org
Elasticsearch Plugins and Rivers
!
Use plugins to extend Elasticsearch functionality
elasticsearch-head, paramedic, bigdesk are all plugins
!
Rivers are pluggable services that pull and index data into
Elasticsearch
Rivers are available for mongodb, couchdb, rabitmq, twitter, wikipedia,
mysql, and etc
!
!
Elasticsearch and Hadoop
Create an external Hive table using ES query q=china
Elasticsearch and Hadoop
External Hive table data – wiki articles that reference the word 'china'
Elasticsearch and Rails
Well supported with the following gems:
!
elasticsearch-rails https://github.com/elasticsearch/elasticsearch-rails
!
elasticsearch-ruby https://github.com/elasticsearch/elasticsearch-ruby
!
searchkick https://github.com/ankane/searchkick
!
tire (retire) https://github.com/karmi/retire
!
!
Elasticsearch and Rails/Ruby
Elasticsearch vs Solr
!
Feature Parity between Elasticsearch & Solr http://solr-vs-elasticsearch.com/
!
Elasticsearch is easier to use and maintain
!
Built from ground up for scale (for all features)
!
Solr - not all features are available in Solr Cloud
!
!
!
!
Advanced Features of Elasticsearch
!
Fuzzy and Proximity Search
!
Autocomplete (term, phrase, completion, and context suggesters)
!
Suggest API
!
Geospatial Search (point, bounding box, polygon)
!
Plugins to extend functionality
!
Scripting in JavaScript, Python, Groovy, and Java
!
!
Advanced Features of Elasticsearch
!
Aggregation (more dimensions than Facets)
!
Related Image Search using LIRE (search similar images based on criteria)
!
Percolator (index queries & match on data - useful for event alert, i.e. back in stock)
!
Re-scoring on query results
!
Polymorphic Search
!
!
!
Elasticsearch the ELK Stack
!
Combining the massively
popular Elasticsearch,
Logstash and Kibana
!
End-to-end stack that
delivers actionable
insights in real-time from
almost any type of
structured and
unstructured data source
!
!
!
Spree + Elasticsearch - you do e-commerce?
!
Elasticsearch compliments or replaces Spree’s built-in search (AR with the ransack gem)
!
Two existing gems spree_elasticsearch and spree_elastic
!
spree_elasticsearch - replace built-in search - i.e. Product.search, etc
spree_elastic - complement built-in search - i.e. Product.elasticsearch
!
Both using model Decorators to add the ES search capabilities
!
Can build on top of one of them, or use the elasticsearch_rails directly
!
!
Elasticsearch and Spree - spree_elastic gem
Elasticsearch and Spree - spree_elasticsearch gem
Elasticsearch Resources
!
http://www.elasticsearch.org/overview/
http://www.elasticsearch.org/guide/
https://github.com/elasticsearch/elasticsearch-hadoop
https://github.com/mobz/elasticsearch-head
http://railscasts.com/episodes/306-elasticsearch-part-1
http://railscasts.com/episodes/307-elasticsearch-part-2
!
!
!
! 🌎 #
Copyright © 2014 Intridea Inc. All rights reserved.
BY WORKING REMOTELY
9,816 Hours Saved
ACROSS 4 COUNTRIES
31 Employees
FOUNDED & STARTED IN 2007
Washington D.C.
We Make.
Designers, developers and project managers; this is who we are.
Building, breaking and solving; this is what we do.
Gracias
Merci 
ありがとう
Danke 
谢谢
Thank You
Copyright © 2014 Intridea Inc. All rights reserved.

More Related Content

What's hot (20)

PDF
Disaggregating Ceph using NVMeoF
ShapeBlue
 
ODP
Elasticsearch presentation 1
Maruf Hassan
 
PPTX
Apache Spark.
JananiJ19
 
PDF
Fast Insight from Fast Data: Integrating ClickHouse and Apache Kafka
Altinity Ltd
 
PDF
Patroni - HA PostgreSQL made easy
Alexander Kukushkin
 
PPTX
Spark architecture
GauravBiswas9
 
PDF
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
Edureka!
 
PDF
Clickhouse Capacity Planning for OLAP Workloads, Mik Kocikowski of CloudFlare
Altinity Ltd
 
PPTX
Managing 2000 Node Cluster with Ambari
DataWorks Summit
 
PDF
jemalloc 세미나
Jang Hoon
 
PPTX
Openstack Swift - Lots of small files
Alexandre Lecuyer
 
PPTX
Introduction to Rust language programming
Rodolfo Finochietti
 
PDF
Introduction to PySpark
Russell Jurney
 
PPTX
How YugaByte DB Implements Distributed PostgreSQL
Yugabyte
 
PPT
Redis
ssuserbad56d
 
PPTX
How to size up an Apache Cassandra cluster (Training)
DataStax Academy
 
PDF
HBaseCon 2015: Taming GC Pauses for Large Java Heap in HBase
HBaseCon
 
PPTX
Apache Spark Architecture | Apache Spark Architecture Explained | Apache Spar...
Simplilearn
 
PDF
Linux Profiling at Netflix
Brendan Gregg
 
PDF
Amazon S3 Best Practice and Tuning for Hadoop/Spark in the Cloud
Noritaka Sekiyama
 
Disaggregating Ceph using NVMeoF
ShapeBlue
 
Elasticsearch presentation 1
Maruf Hassan
 
Apache Spark.
JananiJ19
 
Fast Insight from Fast Data: Integrating ClickHouse and Apache Kafka
Altinity Ltd
 
Patroni - HA PostgreSQL made easy
Alexander Kukushkin
 
Spark architecture
GauravBiswas9
 
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
Edureka!
 
Clickhouse Capacity Planning for OLAP Workloads, Mik Kocikowski of CloudFlare
Altinity Ltd
 
Managing 2000 Node Cluster with Ambari
DataWorks Summit
 
jemalloc 세미나
Jang Hoon
 
Openstack Swift - Lots of small files
Alexandre Lecuyer
 
Introduction to Rust language programming
Rodolfo Finochietti
 
Introduction to PySpark
Russell Jurney
 
How YugaByte DB Implements Distributed PostgreSQL
Yugabyte
 
How to size up an Apache Cassandra cluster (Training)
DataStax Academy
 
HBaseCon 2015: Taming GC Pauses for Large Java Heap in HBase
HBaseCon
 
Apache Spark Architecture | Apache Spark Architecture Explained | Apache Spar...
Simplilearn
 
Linux Profiling at Netflix
Brendan Gregg
 
Amazon S3 Best Practice and Tuning for Hadoop/Spark in the Cloud
Noritaka Sekiyama
 

Viewers also liked (14)

PPTX
Creating global functions
Rahul Kumar
 
PDF
Webrat: Rails Acceptance Testing Evolved
brynary
 
PDF
Elasticsearch at Automattic
Greg Brown
 
PPTX
Mule Expression language
Mani Rathnam Gudi
 
KEY
ElasticSearch at berlinbuzzwords 2010
Elasticsearch
 
KEY
You know, for search. Querying 24 Billion Documents in 900ms
Jodok Batlogg
 
PPTX
Elastic search overview
ABC Talks
 
PDF
Prototyping applications with heroku and elasticsearch
protofy
 
PDF
Introduction to Elasticsearch
Ruslan Zavacky
 
PDF
Introduction to Elasticsearch
Sematext Group, Inc.
 
PPTX
Introduction to Elasticsearch with basics of Lucene
Rahul Jain
 
PDF
Scaling massive elastic search clusters - Rafał Kuć - Sematext
Rafał Kuć
 
PPTX
An Introduction to Elastic Search.
Jurriaan Persyn
 
PPTX
Solr vs. Elasticsearch - Case by Case
Alexandre Rafalovitch
 
Creating global functions
Rahul Kumar
 
Webrat: Rails Acceptance Testing Evolved
brynary
 
Elasticsearch at Automattic
Greg Brown
 
Mule Expression language
Mani Rathnam Gudi
 
ElasticSearch at berlinbuzzwords 2010
Elasticsearch
 
You know, for search. Querying 24 Billion Documents in 900ms
Jodok Batlogg
 
Elastic search overview
ABC Talks
 
Prototyping applications with heroku and elasticsearch
protofy
 
Introduction to Elasticsearch
Ruslan Zavacky
 
Introduction to Elasticsearch
Sematext Group, Inc.
 
Introduction to Elasticsearch with basics of Lucene
Rahul Jain
 
Scaling massive elastic search clusters - Rafał Kuć - Sematext
Rafał Kuć
 
An Introduction to Elastic Search.
Jurriaan Persyn
 
Solr vs. Elasticsearch - Case by Case
Alexandre Rafalovitch
 
Ad

Similar to Using elasticsearch with rails (20)

PPTX
ElasticSearch Basic Introduction
Mayur Rathod
 
PPTX
Elastic search
Binit Pathak
 
ODP
Elasticsearch for beginners
Neil Baker
 
PDF
Wanna search? Piece of cake!
Alex Kursov
 
PDF
Elasticsearch: An Overview
Ruby Shrestha
 
PPTX
Introduction to ElasticSearch
Manav Shrivastava
 
PPTX
Elasticsearch
Divij Sehgal
 
PDF
ElasticSearch: Distributed Multitenant NoSQL Datastore and Search Engine
Daniel N
 
PDF
Workshop: Learning Elasticsearch
Anurag Patel
 
PDF
ElasticSearch Getting Started
Onuralp Taner
 
PDF
Deep dive to ElasticSearch - معرفی ابزار جستجوی الاستیکی
Ehsan Asgarian
 
PDF
Elasticsearch and Spark
Audible, Inc.
 
PPTX
Perl and Elasticsearch
Dean Hamstead
 
PDF
JavaCro'15 - Elasticsearch as a search alternative to a relational database -...
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PPTX
Elasticsearch as a search alternative to a relational database
Kristijan Duvnjak
 
PDF
Intro to Elasticsearch
Clifford James
 
PPTX
Elasticsearch { "Meetup" : "talk" }
Lutf Ur Rehman
 
PPTX
Elastic search
Mahmoud91Tx
 
PDF
Elasticsearch
Shagun Rathore
 
PPTX
Search and analyze your data with elasticsearch
Anton Udovychenko
 
ElasticSearch Basic Introduction
Mayur Rathod
 
Elastic search
Binit Pathak
 
Elasticsearch for beginners
Neil Baker
 
Wanna search? Piece of cake!
Alex Kursov
 
Elasticsearch: An Overview
Ruby Shrestha
 
Introduction to ElasticSearch
Manav Shrivastava
 
Elasticsearch
Divij Sehgal
 
ElasticSearch: Distributed Multitenant NoSQL Datastore and Search Engine
Daniel N
 
Workshop: Learning Elasticsearch
Anurag Patel
 
ElasticSearch Getting Started
Onuralp Taner
 
Deep dive to ElasticSearch - معرفی ابزار جستجوی الاستیکی
Ehsan Asgarian
 
Elasticsearch and Spark
Audible, Inc.
 
Perl and Elasticsearch
Dean Hamstead
 
JavaCro'15 - Elasticsearch as a search alternative to a relational database -...
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Elasticsearch as a search alternative to a relational database
Kristijan Duvnjak
 
Intro to Elasticsearch
Clifford James
 
Elasticsearch { "Meetup" : "talk" }
Lutf Ur Rehman
 
Elastic search
Mahmoud91Tx
 
Elasticsearch
Shagun Rathore
 
Search and analyze your data with elasticsearch
Anton Udovychenko
 
Ad

Recently uploaded (20)

PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PPTX
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
Practical Applications of AI in Local Government
OnBoard
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Kubernetes - Architecture & Components.pdf
geethak285
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 

Using elasticsearch with rails

  • 1. Copyright © 2014 Intridea Inc. All rights reserved. Elasticsearch with Rails July 10, 2014 Tom Zeng Director of Engineering [email protected] @tomzeng www.linkedin.com/in/tomzeng
  • 2. What is Elasticsearch ! Elasticsearch is a “flexible and powerful open source, distributed real-time search and analytics engine for the cloud” More than just full text search, it has powerful analytics capability, and can be used as a NoSQL data store Easy to setup, easy to use, easy to scale, easy to maintain Suitable for projects of any size (large and small, cloud or non-cloud) that need full text search and/or analytics, it’s our preferred search engine for Rails apps
  • 3. Elasticsearch Quick Live Demo Use curl to add some data (local Elasticsearch instance at port 9200) ! curl -X DELETE "http://localhost:9200/todos" (clean up the index and start from scratch) ! curl -X POST "http://localhost:9200/todos/task/1" -d '{"title" : "Learn Elasticsearch", "due_date" : "20140710T00:00:00", "done" : true, "tags" : ["seach","backend"]}' ! curl -X POST "http://localhost:9200/todos/task/2" -d '{"title" : "Learn D3 and Backbone", "due_date" : "20140720T00:00:00", "done" : true, "tags" : ["frontend","javascript","visualization"]}' ! curl -X POST "http://localhost:9200/todos/task/3" -d '{"title" : "Learn Rails 4", "due_date" : "20140830T00:00:00", "done" : false, "tags" : ["backend","ruby","rails"]}' ! curl -X POST "http://localhost:9200/todos/task/4" -d '{"title" : "Learn Backbone Marionette", "due_date" : "20140715T00:00:00", "done" : true, "tags" : ["frontend","javascript"]}' !
  • 4. Elasticsearch Quick Live Demo Use curl to query data curl http://localhost:9200/todos/task/1 (use as K/V store) curl http://localhost:9200/todos/_search?pretty&q=done:false curl http://localhost:9200/todos/_search?pretty&q=tags:backend curl http://localhost:9200/todos/_search?pretty&q=title:back* curl -X POST "http://localhost:9200/todos/task/_search?pretty" -d ' { query : { range : { due_date : { from : "20140701", to : "20140715" } } } }' !
  • 10. Who uses Elasticsearch - Github Sort HighlightFacets Filters Fulltext Search Pagination
  • 11. Elasticsearch Key Concepts Cluster – A cluster consists of one or more nodes which share the same cluster name. Each cluster has a single master node which is chosen automatically by the cluster and which can be replaced if the current master node fails. Node – A node is a running instance of elasticsearch which belongs to a cluster. Multiple nodes can be started on a single server for testing purposes, but usually you should have one node per server. At startup, a node will use unicast (or multicast, if specified) to discover an existing cluster with the same cluster name and will try to join that cluster. Index – An index is like a ‘database’ in a relational database. It has a mapping which defines multiple types. An index is a logical namespace which maps to one or more primary shards and can have zero or more replica shards. Type – A type is like a ‘table’ in a relational database. Each type has a list of fields that can be specified for documents of that type. The mapping defines how each field in the document is analyzed. http://www.elasticsearch.org/guide/reference/glossary
  • 12. Elasticsearch Key Concepts Document – A document is a JSON document which is stored in elasticsearch. It is like a row in a table in a relational database. Each document is stored in an index and has a type and an id. A document is a JSON object (also known in other languages as a hash / hashmap / associative array) which contains zero or more fields, or key-value pairs. The original JSON document that is indexed will be stored in the _source field, which is returned by default when getting or searching for a document. Field – A document contains a list of fields, or key-value pairs. The value can be a simple (scalar) value (eg a string, integer, date), or a nested structure like an array or an object. A field is similar to a column in a table in a relational database. The mapping for each field has a field ‘type’ (not to be confused with document type) which indicates the type of data that can be stored in that field, eg integer, string, object. The mapping also allows you to define (amongst other things) how the value for a field should be analyzed. Mapping – A mapping is like a ‘schema definition’ in a relational database. Each index has a mapping, which defines each type within the index, plus a number of index-wide settings. A mapping can either be defined explicitly, or it will be generated automatically when a document is indexed http://www.elasticsearch.org/guide/reference/glossary
  • 13. Elasticsearch Key Concepts Shard – A shard is a single Lucene instance. It is a low-level “worker” unit which is managed automatically by elasticsearch. An index is a logical namespace which points to primary and replica shards. Elasticsearch distributes shards amongst all nodes in the cluster, and can move shards automatically from one node to another in the case of node failure, or the addition of new nodes. Primary Shard – Each document is stored in a single primary shard. When you index a document, it is indexed first on the primary shard, then on all replicas of the primary shard. By default, an index has 5 primary shards. You can specify fewer or more primary shards to scale the number of documents that your index can handle. Replica Shard – Each primary shard can have zero or more replicas. A replica is a copy of the primary shard, and has two purposes: 1) increase failover: a replica shard can be promoted to a primary shard if the primary fails. 2) increase performance: get and search requests can be handled by primary or replica shards. ! ! http://www.elasticsearch.org/guide/reference/glossary
  • 14. Elasticsearch Key Concepts ! Elasticsearch SQL ! Index => Database Type => Table Document => Row Field => Column Mapping => Schema Shard => Partition ! !
  • 15. Elasticsearch Installation OS X brew install elasticsearch Ubuntu wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.deb sudo dpkg -i elasticsearch-1.1.1.deb Centos wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.noarch.rpm sudo yum install elasticsearch-1.1.1.noarch.rpm !
  • 18. Elasticsearch Monitoring elasticsearch-head - https://github.com/mobz/elasticsearch-head ! ! ! ! Marvel - http://www.elasticsearch.org/guide/en/marvel/current/#_marvel_8217_s_dashboards Paramedic - https://github.com/karmi/elasticsearch-paramedic Bigdesk - https://github.com/lukas-vlcek/bigdesk/ !
  • 20. Elasticsearch API Examples Use curl to run the query and facet APIs curl -X POST "http://localhost:9200/todos/_search?pretty=true" -d ' { "query" : { "query_string" : {"query" : "Learn*"} }, "facets" : { "tags" : { "terms" : {"field" : "tags"} } } } ' Facets – todos tagged with keywords javascript: 2 frontend: 2 backend: 2 visualization: 1 ! !
  • 22. Elasticsearch Query DSL Examples http://www.elasticsearch.org
  • 24. Elasticsearch Query DSL Examples http://www.elasticsearch.org
  • 25. Elasticsearch Plugins and Rivers ! Use plugins to extend Elasticsearch functionality elasticsearch-head, paramedic, bigdesk are all plugins ! Rivers are pluggable services that pull and index data into Elasticsearch Rivers are available for mongodb, couchdb, rabitmq, twitter, wikipedia, mysql, and etc ! !
  • 26. Elasticsearch and Hadoop Create an external Hive table using ES query q=china
  • 27. Elasticsearch and Hadoop External Hive table data – wiki articles that reference the word 'china'
  • 28. Elasticsearch and Rails Well supported with the following gems: ! elasticsearch-rails https://github.com/elasticsearch/elasticsearch-rails ! elasticsearch-ruby https://github.com/elasticsearch/elasticsearch-ruby ! searchkick https://github.com/ankane/searchkick ! tire (retire) https://github.com/karmi/retire ! !
  • 30. Elasticsearch vs Solr ! Feature Parity between Elasticsearch & Solr http://solr-vs-elasticsearch.com/ ! Elasticsearch is easier to use and maintain ! Built from ground up for scale (for all features) ! Solr - not all features are available in Solr Cloud ! ! ! !
  • 31. Advanced Features of Elasticsearch ! Fuzzy and Proximity Search ! Autocomplete (term, phrase, completion, and context suggesters) ! Suggest API ! Geospatial Search (point, bounding box, polygon) ! Plugins to extend functionality ! Scripting in JavaScript, Python, Groovy, and Java ! !
  • 32. Advanced Features of Elasticsearch ! Aggregation (more dimensions than Facets) ! Related Image Search using LIRE (search similar images based on criteria) ! Percolator (index queries & match on data - useful for event alert, i.e. back in stock) ! Re-scoring on query results ! Polymorphic Search ! ! !
  • 33. Elasticsearch the ELK Stack ! Combining the massively popular Elasticsearch, Logstash and Kibana ! End-to-end stack that delivers actionable insights in real-time from almost any type of structured and unstructured data source ! ! !
  • 34. Spree + Elasticsearch - you do e-commerce? ! Elasticsearch compliments or replaces Spree’s built-in search (AR with the ransack gem) ! Two existing gems spree_elasticsearch and spree_elastic ! spree_elasticsearch - replace built-in search - i.e. Product.search, etc spree_elastic - complement built-in search - i.e. Product.elasticsearch ! Both using model Decorators to add the ES search capabilities ! Can build on top of one of them, or use the elasticsearch_rails directly ! !
  • 35. Elasticsearch and Spree - spree_elastic gem
  • 36. Elasticsearch and Spree - spree_elasticsearch gem
  • 38. ! 🌎 # Copyright © 2014 Intridea Inc. All rights reserved. BY WORKING REMOTELY 9,816 Hours Saved ACROSS 4 COUNTRIES 31 Employees FOUNDED & STARTED IN 2007 Washington D.C. We Make. Designers, developers and project managers; this is who we are. Building, breaking and solving; this is what we do.
  • 39. Gracias Merci ありがとう Danke 谢谢 Thank You Copyright © 2014 Intridea Inc. All rights reserved.