79
Introduction elasticsearch @somkiat SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Introduction to elasticsearch fast lane

Embed Size (px)

Citation preview

Page 1: Introduction to elasticsearch fast lane

Introduction elasticsearch

@somkiat

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Page 2: Introduction to elasticsearch fast lane

realtime, search and analytics

engine

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

LuceneDistributed

Scalability

open-sourcedocumentation

JSONAPI

RESTFul

document store

High-Availability

JAVA

Plug-ins

Page 3: Introduction to elasticsearch fast lane

History

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

First version 0.4 in Feb 2010Rewrite from Compass projectAdd scalability

Created by Shay Banon

Current version 1.7.1

Page 4: Introduction to elasticsearch fast lane

Who use ?

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

https://www.elastic.co/use-cases

Page 5: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

• Search repositories, users, issues, pull request• Search sourcecode 130 พันล้านบรรทัด• Track alerts, events และ logs

Page 6: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

• ใ้ Full text seach + geolocation• ใ้ feature More-like-this ในการหาคําถาม และ คําตอบ

Page 7: Introduction to elasticsearch fast lane

Clients

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

• Java• PHP• Ruby• Python• JavaScript

• Go• Scala• .Net• Clojure• Erlang

• NodeJS • R

http://www.elasticsearch.org/guide/en/elasticsearch/client/community/current/clients.html

Page 8: Introduction to elasticsearch fast lane

https://github.com/github-sprint3r/elasticsearch_workshop

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Page 9: Introduction to elasticsearch fast lane

Let’s start

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Page 10: Introduction to elasticsearch fast lane

Installation …

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Page 11: Introduction to elasticsearch fast lane

elasticsearch

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

• real-time• distributed• search• analytics

Page 12: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

mapping

analysis query dsl

Page 13: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

query dsl• flexible• powerful• query language

Page 14: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

queries• relevance

filters• boolean yes/no

• full text • exact values• not cached • cached• slower • faster

Page 15: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Filter firstquery remaining documents

Page 16: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search

Page 17: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { . . . }}

Page 18: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “match” : { “title” : “search” } }}

Page 19: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “match_all” : { } }}

Page 20: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “filtered” : { “query” : { … }, “filter” : { … } } }}

Page 21: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “filtered” : { “query” : { “match” : { “title” : “search” } }, “filter” : { … } } }}

Page 22: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “filtered” : { “query” : { “match” : { “title” : “search” } }, “filter” : { “term” : { “status” : “active” } } } }}

Page 23: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “filtered” : { “query” : { “match_all” : { } }, “filter” : { “term” : { “status” : “active” } } } }}

Page 24: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “filtered” : { “query” : { “match_all” : { } }, “filter” : { “term” : { “status” : “active” } } } }}

Page 25: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

How data is indexed ?

Page 26: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

{ "title": "Quick brown rabbits", "content": "Brown rabbits are commonly seen" }

{ "title": "Keeping pets healthy", "content": "My quick brown fox eats rabbits on a regular basis" }

Page 27: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

where content like

“%brown%fox%”

Page 28: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Analysis

Page 29: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

{ "title": "Quick brown rabbits", "content": "Brown rabbits are commonly seen" }

{ "title": "Keeping pets healthy", "content": "My quick brown fox eats rabbits on a regular basis" }

Page 30: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

{ "title": [quick,brown,rabbits], "content": [brown,rabbits,are,commonly,seen] }

{ "title": [keeping,pets,healthy], "content": [my,quick,brown,fox,eats,rabbits,on ,a,regular,basis] }

Page 31: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

field: content Term Document 1 Document 2

aare

basisbrown

commonlyeatsfoxmyon

quick rabbitsregularseen

Page 32: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

field: content Term Document 1 Document 2

aare

basisbrown

commonlyeatsfoxmyon

quick rabbitsregularseen

Page 33: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Inverted indexed

Page 34: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

where field = “value”

Page 35: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

where field contains “value”

Page 36: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

where field contains “value”term filter

“term” : { “title” : “brown” }

Page 37: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “filtered” : { “query” : { “match_all” : { } }, “filter” : { “term” : { “title” : “brown” } } } }}

Page 38: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

field: title Term Document 1 Document 2

brown

healthy

keeping

pets

quick

rabbits

Page 39: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

field: title Term Document 1 Document 2

brown

healthy

keeping

pets

quick

rabbits

Page 40: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Result

bitset[ 1, 0 ]cache as “title:brown”

Page 41: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

where field IN [“value1”, … ]

Page 42: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

terms filter

“terms” : { “title” : [“quick”, “pets”] }

where field IN [“value1”, … ]

Page 43: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

field: title Term Document 1 Document 2

brown

healthy

keeping

pets

quick

rabbits

Page 44: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Result

bitset[ 1, 1 ]cache as “title:quick title:pets”

Page 45: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

where field >= “value 1” and field < “value 2”

Page 46: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

where field >= “value 1” and field < “value 2”

range filter

Page 47: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

“range” : { “content” : { “gte” : “a”, “lt” : “m”

} }

Page 48: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

field: content Term Document 1 Document 2

aare

basisbrown

commonlyeatsfoxmyon

quick rabbitsregularseen

Page 49: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Result

bitset[ 1, 1 ]cache as “content:[a TO m]”

Page 50: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

AND … OR … NOT

Page 51: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

AND … OR … NOTbool filter

Page 52: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

“bool” : { “must” : [ <filters> ], “should” : [ <filters> ], “must_not” : [ <filters> ], }

Page 53: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

how relevant is this term ?

Page 54: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

how relevant is this term ?term query

Page 55: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “term” : { “title” : “brown” } }}

Page 56: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

how relevant is this document ?

Page 57: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Relevant score

Term in this documentmore is better

Page 58: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Relevant score

Term in all documentsless is better

Page 59: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Relevant score

How long of this document ?shorter is better

Page 60: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

AND … OR … NOTbool query

Page 61: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

“bool” : { “must” : [ <filters> ], #AND “should” : [ <filters> ], ??? “must_not” : [ <filters> ], #NOT }

Page 62: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

“bool” : { “must” : [ <filters> ], #AND “should” : [ <filters> ], ??? “must_not” : [ <filters> ], #NOT “minimum_should_match” : ? }

Page 63: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

bool filter -> True | False

bool query -> _score

Page 64: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

match query

Page 65: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

match queryhigh level query

understand mapping and analysis

Page 66: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

match query1. analyze query string2. rewrite query

Page 67: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

{ “match” : { “title” : “QUICK!” } }

title:quick

{ “term” : { “title” : “quick” } }

Page 68: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

{ “match” : { “title” : “QUICK FOX!” } }

title:quick OR title:fox

{ “bool” : { “should” : [ { “term” : { “title” : “quick” } }, { “term” : { “title” : “fox” } } ] }}

Page 69: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Need all words must match ?

Page 70: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “term” : { “title” : “QUICK FOX!” } }}

Page 71: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “term” : { “title” : { “query” : “QUICK FOX!”, “operator” : “and” } } }}

Page 72: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ "query": {

"bool": { "must": [

{ "term": { "title" : "quick" } }, { "term": { "title" : "fox" } }

] }

} }

Page 73: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “match_pharse” : { “title” : { “query” : “QUICK BROWN” } } }}

Page 74: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “match_pharse” : { “title” : { “query” : “QUICK RABBITS” } } }}

Page 75: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “match_pharse” : { “title” : { “query” : “BROWN QUICK” } } }}

Page 76: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Limit long tail ?

Page 77: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ “query” : { “term” : { “title” : { “query” : “QUICK BROWN FOX!”, “minimum_should_match” : “75”% } } }}

Page 78: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

GET /_search{ "query": {

"bool": { "should": [

{ "term": { "title" : "quick" } }, { "term": { "title" : "brown" } }, { "term": { "title" : "fox" } }

], “minimum_should_match” : 2 }

} }

Page 79: Introduction to elasticsearch fast lane

SPRINT3R Siam Chamnankit Co., Ltd., Odd-e (Thailand) Co., Ltd. and Alliance

Aggregation APIanalytic your dataexplore your data