Slide 1

Slide 1 text

Elasticsearch Tony Messias MaceioDevMeetup #5

Slide 2

Slide 2 text

who am I? ➔ Tony Messias ~ @tony0x01 ➔ Building web stuff since ~2010

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

search

Slide 5

Slide 5 text

what is “search”? “A search is the organized pursuit of information (...) that you want to find, but you have no idea where it is.” (NASA).

Slide 6

Slide 6 text

how do we add search to our apps? ➔ SQL queries (not just LIKE queries); ➔ Elasticsearch; ➔ Lucene; ➔ Solr; ➔ …

Slide 7

Slide 7 text

how do we add search to our apps? ➔ SQL queries (not just LIKE queries); ➔ Elasticsearch; ➔ Lucene; ➔ Solr; ➔ …

Slide 8

Slide 8 text

who uses it?

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

what is Elasticsearch? ➔ database server; ➔ document based; ➔ based on Apache Lucene; ➔ schema-free*;

Slide 11

Slide 11 text

why Elasticsearch? ➔ speaks HTTP/JSON; ➔ easy to setup; ➔ data replication; ➔ easily scalable;

Slide 12

Slide 12 text

what can I use it for?

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

but it can do more than that…

Slide 15

Slide 15 text

some analogy * Relational ES database index table type row document column field schema mapping index everything SQL QueryDSL

Slide 16

Slide 16 text

REST(ish) $ curl -XPOST http://es-server.com/index/type/id -d '{}'

Slide 17

Slide 17 text

REST(ish) $ curl -XPOST http://es-server.com/index/type/id -d '{}'

Slide 18

Slide 18 text

REST(ish) $ curl -XPOST http://es-server.com/index/type/id -d '{}'

Slide 19

Slide 19 text

REST(ish) $ curl -XPOST http://es-server.com/index/type/id -d '{}'

Slide 20

Slide 20 text

REST(ish) $ curl -XPOST http://es-server.com/index/type/id -d '{}'

Slide 21

Slide 21 text

enough with the talking… let’s do some searches!

Slide 22

Slide 22 text

but first, let’s talk about CRUD

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

indexing

Slide 25

Slide 25 text

{ "email": "[email protected]", "first_name": "John", "last_name": "Smith", "info": { "bio": "Eco-warrior and defender of the weak", "age": 25, "interests": [ "dolphins", "whales" ] }, "join_date": "2014/05/01" }

Slide 26

Slide 26 text

$ curl -XPOST localhost:9200/my-app/users -d ‘{ "email": "[email protected]", "first_name": "John", "last_name": "Smith", "info": { "bio": "Eco-warrior and defender of the weak", "age": 25, "interests": [ "dolphins", "whales" ] }, "join_date": "2014/05/01" }’

Slide 27

Slide 27 text

{ "_index": "my-app", "_type": "users", "_id": "AUxOBeJbue-jeR3jNcHj", "_version": 1, "created": true }

Slide 28

Slide 28 text

$ curl -XPUT localhost:9200/my-app/users/AUxOBeJbue-jeR3jNcHj -d ‘{ "email": "[email protected]", "first_name": "John" }’

Slide 29

Slide 29 text

{ "_index": "my-app", "_type": "users", "_id": "AUxOBeJbue-jeR3jNcHj", "_version": 2, "created": false }

Slide 30

Slide 30 text

$ curl -XGET localhost:9200/my-app/users/AUxOBeJbue-jeR3jNcHj { "_index": "my-app", "_type": "users", "_id": "AUxOJe-4ue-jeR3jNcHo", "_version": 2, "found": true, "_source": { "email": "[email protected]", "first_name": "John" } }

Slide 31

Slide 31 text

$ curl -XDELETE localhost:9200/my-app/users/AUxOBeJbue-jeR3jNcHj { "found": true, "_index": "my-app", "_type": "users", "_id": "AUxOBeJbue-jeR3jNcHj", "_version": 3 }

Slide 32

Slide 32 text

searching

Slide 33

Slide 33 text

$ curl -XPOST localhost:9200/my-app/users/_search -d ‘{ "query": { "match": { "first_name": "John" } } }’

Slide 34

Slide 34 text

{ ..., "hits": { "total": 4, "max_score": 0.30685282, "hits": [ { "_index": "my-app", "_type": "users", "_id": "AUxOJWlCue-jeR3jNcHn", "_score": 0.30685282, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Doe", "info": { "bio": "Like rock music", "age": 35, "interests": [ "dolphins", "whales" ] }, "join_date": "2014/05/01" } },...

Slide 35

Slide 35 text

$ curl -XPOST localhost:9200/my-app/users/_search -d ‘{ "query": { "filtered": { "query": { "match": { "first_name": "John" } }, "filter": { "range": { "info.age": { "gt": 34 } } } } } }’

Slide 36

Slide 36 text

{ "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.30685282, "hits": [ { "_index": "my-app", "_type": "users", "_id": "AUxOJWlCue-jeR3jNcHn", "_score": 0.30685282, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Doe", "info": { "bio": "Like rock music", "age": 35, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" } } ] } }

Slide 37

Slide 37 text

queries vs. filters

Slide 38

Slide 38 text

queries vs. filters

Slide 39

Slide 39 text

full-text search

Slide 40

Slide 40 text

$ curl -XPOST localhost:9200/my-app/users/_search -d ‘{ "query": { "match": { "info.bio": "rock climbing" } } }’

Slide 41

Slide 41 text

{ "took": 9, "timed_out": false, "_shards": {...}, "hits": { "total": 2, "max_score": 0.2169777, "hits": [ { "_index": "my-app", "_type": "users", "_id": "AUxOJe-4ue-jeR3jNcHo", "_score": 0.2169777, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Kennedy", "info": { "bio": "I love rock climbing!", "age": 29, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" } }, { "_index": "my-app", "_type": "users", "_id": "AUxOJWlCue-jeR3jNcHn", "_score": 0.02250402, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Doe", "info": { "bio": "Like rock music", "age": 35, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" } } ] } }

Slide 42

Slide 42 text

$ curl -XPOST localhost:9200/my-app/users/_search -d ‘{ "query": { "match_phrase": { "info.bio": "rock climbing" } } }’

Slide 43

Slide 43 text

{…, "hits": { "total": 1, "max_score": 0.30685282, "hits": [ { "_index": "my-app", "_type": "users", "_id": "AUxOJe-4ue-jeR3jNcHo", "_score": 0.30685282, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Kennedy", "info": { "bio": "I love rock climbing!", "age": 29, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" } } ] } }

Slide 44

Slide 44 text

$ curl -XPOST localhost:9200/my-app/users/_search -d ‘{ "query": { "match_phrase": { "info.bio": "rock climbing" } }, "highlight": { "fields": { "info.bio": {} } } }’

Slide 45

Slide 45 text

... { "_index": "my-app", "_type": "users", "_id": "AUxOJe-4ue-jeR3jNcHo", "_score": 0.30685282, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Kennedy", "info": { "bio": "I love rock climbing!", "age": 29, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" }, "highlight": { "info.bio": ["I love rock climbing!"] } } ] } }

Slide 46

Slide 46 text

is this it?

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

analytics

Slide 50

Slide 50 text

aggregations

Slide 51

Slide 51 text

buckets metrics

Slide 52

Slide 52 text

$ curl -XPOST localhost:9200/my-app/users/_search?search_type=count -d'{ "aggs": { "all_interests": { "terms": { "field": "info.interests" } } } }'

Slide 53

Slide 53 text

{ ..., "hits": {"total": 4,"max_score": 0,"hits": []}, "aggregations": { "all_interests": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "whales", "doc_count": 4 }, { "key": "dolphins", "doc_count": 3 } ] } }}

Slide 54

Slide 54 text

$ curl -XPOST localhost:9200/my-app/users/_search?search_type=count -d'{ "aggs": { "all_interests": { "terms": { "field": "info.interests" } } } }'

Slide 55

Slide 55 text

$ curl -XPOST localhost:9200/my-app/users/_search?search_type=count -d'{ "aggs": { "all_interests": { "terms": { "field": "info.interests" }, "aggs": { "avg_age": { "avg": { "field": "info.age" } } } } } }'

Slide 56

Slide 56 text

{ …, "aggregations": { "all_interests": { …, "buckets": [ { "key": "whales", "doc_count": 4, "avg_age": { "value": 31.5 } }, { "key": "dolphins", "doc_count": 3, "avg_age": { "value": 32.333333333333336 } } ] }}}

Slide 57

Slide 57 text

$ curl -XPOST localhost:9200/my-app/users/_search?search_type=count -d'{ "query": { "match_phrase": { "info.bio": "rock climbing" } }, "aggs": { "all_interests": { "terms": { "field": "info.interests" }, "aggs": { "avg_age": { "avg": { "field": "info.age" } } } } }}'

Slide 58

Slide 58 text

{ …, "aggregations": { "all_interests": { …, "buckets": [ { "key": "whales", "doc_count": 1, "avg_age": { "value": 29 } } ] }}}

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

questions?

Slide 61

Slide 61 text

Resources ➔ http://www.elastic. co/guide/en/elasticsearch/guide/current/index. html ➔ https://leanpub.com/elasticsearch-quick-start ➔ https://www.elastic.co/use-cases ➔ https://speakerdeck.com/asm89/elasticsearch

Slide 62

Slide 62 text

Resources ➔ https://speakerdeck. com/johnbeynon/elasticsearch-101 ➔ http://blog.madewithlove.be/post/integrating- elasticsearch-with-your-laravel-app/ ➔ http://blog.madewithlove.be/post/elasticsearch- aggregations/