Slide 1

Slide 1 text

ZERO TO HERO ELASTIC

Slide 2

Slide 2 text

CONFIGURATION ▸ 1. https://github.com/skowron-line/phpers2019-workshop ▸ 2. docker-compose up -d

Slide 3

Slide 3 text

DOCKER-COMPOSE PS

Slide 4

Slide 4 text

HTTP://LOCALHOST:9200

Slide 5

Slide 5 text

HTTP://LOCALHOST:5601

Slide 6

Slide 6 text

SQL VS ELASTIC Elastic SQL Field Column Document Row Index Table Database Cluster

Slide 7

Slide 7 text

CREATE INDEX PUT {index}

Slide 8

Slide 8 text

ADD DOCUMENT POST {index}/_doc

Slide 9

Slide 9 text

SAMPLE RESPONSE { "_index" : "test", "_type" : "_doc", "_id" : "H9wYAG0Bjhby-D6cxV_8", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }

Slide 10

Slide 10 text

UPDATE DOCUMENT PUT {index}/_doc/{id}

Slide 11

Slide 11 text

SAMPLE RESPONSE { "_index" : "test", "_type" : "_doc", "_id" : "H9wYAG0Bjhby-D6cxV_8", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }

Slide 12

Slide 12 text

DELETE DOCUMENT DELETE {index}/_doc

Slide 13

Slide 13 text

SAMPLE RESPONSE { "_index" : "test", "_type" : "_doc", "_id" : "H9wYAG0Bjhby-D6cxV_8", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }

Slide 14

Slide 14 text

While using the _version might seem to work in certain cases, I would recommend to never use it for anything else than optimistic locking of updates. In particular, versions do not carry any meaning: they might look like the number of times a document has been modified but it is not always the case (for instance if you create a new document which has the same ID as a document that you just deleted, the version number of the new document will not be 1), and more importantly it is an implementation detail, this behaviour might change in the future. Versioning

Slide 15

Slide 15 text

BULK curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d' { "create" : { "_index" : "test", "_id" : "1" } } {“field”: “value”, “field”: “value”} { "delete" : { "_index" : "test", "_id" : "1" } } { "update" : {"_id" : "1", "_index" : "test"} } { "doc" : {"field2" : "value2"} }

Slide 16

Slide 16 text

ANALYSING PROCESS Text -> Char filter -> Tokenizer -> Token filter -> Inverted index

Slide 17

Slide 17 text

CHAR FILTER The character filter has the ability to perform addition, removal or replacement actions on the input text given to them.

Slide 18

Slide 18 text

CHAR FILTER “Phpers Summit” “Phpers summit”

Slide 19

Slide 19 text

TOKENIZER A tokenizer receives a stream of characters, breaks it up into individual tokens (usually individual words), and outputs a stream of tokens

Slide 20

Slide 20 text

TOKENIZER “Phpers summit najlepsza konferencja w Polsce” “Phpers”, “summit”, “najlepsza”, “konferencja”, “w”, “Polsce”

Slide 21

Slide 21 text

TOKEN FILTER Token filters can act on the tokens generated from the tokenizers and modify, add or remove them

Slide 22

Slide 22 text

ANALYZER Analyzer is combination of tokenizer and token filters

Slide 23

Slide 23 text

BUILD IN ANALYZERS ‣ standard (default) combination of (standard token filter, lowercase and stop token filter) ‣ simple ‣ whitespace ‣ stop ‣ keyword ‣ pattern ‣ language ‣ fingerprint

Slide 24

Slide 24 text

NORMALIZER Normalizers are similar to analyzers except that they may only emit a single token

Slide 25

Slide 25 text

INVERTED INDEX Frontend developer w Warszawie PHP Developer Poznań

Slide 26

Slide 26 text

INVERTED INDEX Token Document 1 Document 2 frontend 1 php 1 developer 1 1 poznan 1 warszawa 1

Slide 27

Slide 27 text

MAPPING Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. For instance, use mappings to define: ‣ which string fields should be treated as full text fields. ‣ which fields contain numbers, dates, or geolocations. ‣ the format of date values. ‣ custom rules to control the mapping for dynamically added fields.

Slide 28

Slide 28 text

MAPPING "mappings": { "properties": { "age": { "type": "integer" }, "email": { "type": "keyword" }, "name": { "type": "text" } } }

Slide 29

Slide 29 text

MAPPING PUT {index} { "mappings": { "properties": { "age": { "type": "integer" }, ... } } }

Slide 30

Slide 30 text

DYNAMIC MAPPING { "mappings": { "dynamic_date_formats": ["MM/dd/yyyy"] } }

Slide 31

Slide 31 text

DYNAMIC MAPPING TEMPLATES "dynamic_templates": [ { "strings": { "match_mapping_type": "string", "mapping": { "type": "text", "analyzer": "whitespace" } } } ]

Slide 32

Slide 32 text

BASIC QUERIES ‣ Match ‣ Multi_match ‣ Term / terms ‣ Range ‣ Wildcard ‣ Query_string ‣ Function_score ‣ Script_score

Slide 33

Slide 33 text

BOOL QUERIES and, or / must, should

Slide 34

Slide 34 text

BOOL QUERIES title = "php developer" AND city = "warszawa" { "query": { "must": [ { "term": { "title": { "value": "php developer" } } }, { "term": { "city": { "value": "warszawa" } } } ] } }

Slide 35

Slide 35 text

LETS GO!