SlideShare a Scribd company logo
ELASTICSEARCH
SEARCH & ANALYZE DATA IN REAL TIME*
Piotr Pelczar • github • stackoverflow
Wrocław 2017, Eurobank
freeimages.com v 1.2
AGENDA
You will find out:
• purpose
• how data is stored and searched
• features + 3rd party
• architecture
• usecase on production
AGENDA
You will not find out:
• production ready configuration: HA, repl, sharding
• monitoring
• production internal bottlenecks / failure recovery
• ELK stack – elasticsearch + logstash + kibana
• comparison of ES i Solr/Sphinx
PURPOSE
• NoSQL
• Databse for full text search
• More reads then writes
every document update is a creation of a new one
• No transactions – BASE instead of ACID
FULL-TEXT SEARCH
• full-text search (FTS) refers to techniques of efficient
search the data simillar to natural language text
• search is performed using full text index
• under the hood ES is powered by Apache Lucene
FULL-TEXT SEARCH
FTS is available in Oracle, MsSQL, MySQL, but...
• KILLER FEATURE: ES enables to customize the proces
of building the full text index
• features like:
– autocomplete
– „Did you mean?” based Levenstein distance
– indexing one field in a several ways
ES IS POWERED BY LUCENE
Features added:
• clustering
• sharding (horizontal scaling)
• replication (copy of shards)
• versioning
• non-full-text indices
• REST API
https://pl.pinterest.com/pin/528328600014757803/
BASE vs ACID
• Atomicity
• Consistency
• Isolation
• Durability
• Basically Available
• Soft state
• Eventual consistency
http://wallpaperswide.com/domino_effect-wallpapers.html
TERMINOLOGY
• Cluster
• Node
• Index (collection of docs) <-> (?) tablespace/database
• Type (partition of index) <-> (?) table/collection
• Document (JSON format)
• Shard & Replicas
https://wallpaperscraft.com/download/london_philharmonic_orchestra_scene_show_play_conductor_8925/2560x1080
DOCUMENT-ORIENTED
I SCHEMA-FREE
• data is stored as documents
• documents are unstructured *
* by default, but there is a possibility to require strict o partly strict schema in the type
definition in index
• all fields are indexed in full-text by default *
* this behaviour is fully configurable, data type can be changed or
the field can be ignored in full-text index
http://www.shximai.com/education-wallpapers.html
INVERTED INDEX
RANKING FORMULA
BM25 similarity function
https://www.slideshare.net/Hadoop_Summit/t-435p212leauv2
1. http://ipl.cs.aueb.gr/stougiannis/bm25_2.html
2. https://en.wikipedia.org/wiki/Tf%E2%80%93idf
q - query
t – term
d - document
ANALYZERS
Analysis - the process of converting text into tokens or
terms which are added to the inverted index for
searching. Analysis is performed by an analyzer.
• Index time analyser
• Search time analyser
http://wallpaperswide.com/glasses_and_book-wallpapers.html
ANALYZERS
1. Tokenizing a block into terms
2. Normalizing (reducing) terms into root form
• Every field in document type can have own analyser
• Fields can be indexed by several analysers
(multi-fields)
https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-
fields.html#_multi_fields_with_multiple_analyzers
http://wallpaperswide.com/glasses_and_book-wallpapers.html
ANALYZERS
1. Character Filters
– html and entities
– triming
2. Tokenizer
3. Token Filters
– stopwords
– Stemmer (root form)
– Phonetic, n-grams
– Synonim
– Patten capture
http://wallpaperswide.com/glasses_and_book-wallpapers.html
Input
Filter
Tokenizer
Token
filter
Index
ANALYZERS
Polish analyser – Stempel
sudo bin/plugin install analysis-stempel
http://wallpaperswide.com/glasses_and_book-wallpapers.html
CUSTOM ANALYSERS
PUT /index_name
{
"settings": {
"analysis": {
"analyzer": {
"polskie_slowa": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "stopwords_polska", "polish_stem", "asciifolding"],
"char_filter": ["html_strip"]
}
},
"filter": {
"stopwords_polska": {
"type": "stop",
"stopwords": ["a", "aby", ...]
}
}
}
}
// ...
TESTING ANALYSER
GET /_analyze
{
"analyzer": "standard",
"text": "Text to analyze"
}
{
"tokens": [
{
"token": "text",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 1
},
// ..
INSERT
PUT /website/blog/123
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}
Custom ID - PUT
POST /website/blog/
Auto ID - POST
INSERT
By default:
• There is no document schema
• All fields are indexed in full-text index
• Type of encountered previously unknown field
is determined by the first value that appeared
(dynamic mapping)
https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html
https://studyinthestates.dhs.gov/2017/01/new-interim-final-guidance-open-for-comment-until-feb-27
INSERT – DYNAMIC MAPPING
dyanmic
- true (fields out of schema are allowed)
- false (new fields are just ignored)
- strict (exception, when unknown field)
https://studyinthestates.dhs.gov/2017/01/new-interim-final-guidance-open-for-comment-until-feb-27
INSERT – DYNAMIC MAPPING
PUT /my_index
{
"mappings": {
"my_type": {
"dynamic": "strict",
"properties": {
"title": { "type": "string"},
"stash": {
"type": "object",
"dynamic": true
}
}
}
}
}
MAPPING
Data types:
• Core
– numeric, date, boolean, binary, string (keyword, fulltext)
• Complex
– array, object, nested (array of objects)
– geo
• Multi-fields
– e.x. date as raw date and full-text value (movie title YEAR), or field
with multiple analysers
• Specialized
– ip, completion, …
MAPPING
"mappings": {
"news": {
"dynamic": "strict",
"properties": {
"title": {
"type": "string",
"analyzer": "polskie_slowa",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"date_published": {
"type": "date",
"index": "not_analyzed"
},
multifield
MAPPING
"mappings": {
"news": {
"dynamic": "strict",
"properties": {
"stories": {
"type": "nested",
"dynamic": "strict",
"properties": {
"id": {
"type": "long",
"index": "not_analyzed"
},
"title": {
"type": "string",
"analyzer": "polskie_slowa"
}
}
}
MAPPING
"mappings": {
"news": {
"dynamic": "strict",
"properties": {
"media": {
"type": "object",
"dynamic": "strict",
"properties": {
"gallery_has": {
"type": "boolean",
"index": "not_analyzed"
},
"video_has": {
"type": "boolean",
"index": "not_analyzed"
},
"poll_has": {
"type": "boolean",
"index": "not_analyzed"
},
GET
GET /website/blog/123
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"found" : true,
"_source" : {
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}
}
Status code: 200, 404
DELETE
DELETE /website/blog/123
UPDATE
PUT /website/blog/123
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}
POST /website/blog/123/_update
{
"title": "My first blog entry„
}
UPDATE – IMMUTABLE DOCS
• Documents are immutable, because Lucene
segments are immutable
• The new version of documents are created
• Old version is marked in .del file
• Previous version is still searchable, but is
removed from search result in the runtime
until cleanup process
https://studyinthestates.dhs.gov/2017/01/new-interim-final-guidance-open-for-comment-until-feb-27
URI SEARCH
GET /index/_search?q=user:kimchy
GET /index/type1,type2/_search?q=user:kimchy
GET /index1,index2/type/_search?q=user:kimchy
GET /_all/type/_search?q=user:kimchy
GET /_search?q=user:kimchy
URI SEARCH
{
"timed_out": false,
"took": 62,
"hits":{
"total" : 1,
"max_score": 1.3862944,
"hits" : [
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "0",
"_score": 1.3862944,
"_source" : {
"user" : "kimchy",
"date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch",
"likes": 0
}
TERM QUERY
GET /index/type/_search
{
"query" : {
"term" : { "user" : "kimchy" }
}
}
SEARCH
Search segments:
• must-match
• should-match (scoring)
• fuzzy-query (Levenstein desitnation)
• filter (without scoring, very fast)
• limit/offset
TERM QUERY & BOOST
"query": {
"bool": {
"should" : [
{
"match": { "title": { "query": "myśliwy", "boost": 1 }
},
],
"filter": {
"and" : [
{
"term": {
"media.gallery_has": false
}
}
]
}
HIGHLIGHTING
GET /_search
{
"query" : {
"match": { "content": "kimchy" }
},
"highlight" : {
"fields" : {
"content" : {}
}
}
}
SUGGESTING
POST music/_search?pretty
{
"suggest": {
"song-suggest" : {
"prefix" : "nir",
"completion" : {
"field" : "suggest"
}
}
}
}
SUGGESTING
"suggest": {
"song-suggest" : [ {
"text" : "nir",
"offset" : 0,
"length" : 3,
"options" : [ {
"text" : "Nirvana",
"_index": "music",
"_type": "song",
"_id": "1",
"_score": 1.0,
"_source": {
"suggest": ["Nevermind", "Nirvana"]
}
} ]
SCORE FUNCTIONS
• Weight
• Field Value factor
• Decay functions
https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html
SCORE FUNCTIONS - DECAY
Model functions:
• Gauss
• Exp
• Linear
• Multivalue
– 2d
– 3d
– n …
SCORE FUNCTIONS - DECAY
"function_score": {
"query": { ... },
"score_mode": "multiply", // how functions are compared
"boost_mode": "multiply", // how functions has impact to
original score
"functions": {
"date_published": {
"origin": " ... "
"offset": " ... "
"scale": " ... "
}
}
CHANGE
https://www.entrepreneur.com/article/269669
REINDEX
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
Every index change needs index rebuild (reindex) :
• Change data types, schema
• Analyser modification
• Shard key, numer of shards (no way to rebalance data)
REINDEX
POST _reindex
{
"source": {
"index": "twitter",
"type": "tweet",
"query": {
"term": {
"user": "kimchy"
}
}
},
"dest": {
"index": "new_twitter"
}
}
2x more space is needed, but there is the possibility to
do query width limit and offset and delete data from
old index in the meantime.
Application have to query
both indices/aliases during
reindex process and filter
duplicates in the runtime.
Index Aliases and Zero Downtime
POST /_aliases
{
"actions" : [
{ "remove" : { "index" : "news_v1",
"alias" : "news_view" } },
{ "add" : { "index" : "news_v2",
"alias" : "news_view" } }
]
}
https://www.elastic.co/guide/en/
elasticsearch/guide/current/index-aliases.html
* REAL TIME, NEAR REALTIME (NRT)
This means is there is a slight latency
(normally one second) from the time you index a
document until the time it becomes searchable.
http://all-free-download.com/wallpapers/animals/horse_racing_wallpaper_horses_animals_wallpaper_382.html
NEAR REALTIME (NRT)
1. Data have to be analysed
2. There are caveats with Lucene segments
NRT i BASE
In the system there is no global-lock
-> append-only, immutable
-> no transactions
-> >> response, availability => PROFIT!
-> data could be not visible immediately
Stale data, but any data...
http://wallpaperswide.com/domino_effect-wallpapers.html
NEAR REALTIME (NRT)
• New index is created in new Lucene Segments periodicaly
• Creation new Lucene Segments are called refresh
• 1 sec by default - NRT
• INSERT -> GET can respond 404.
NEAR REALTIME (NRT)
• Lucene allows to open a new Segment and search in it
without Commit
• Commit makes Lucene Segment immutable
• Document is always added to a new Segment that temporarily
resides in memory (file system cache) and are searchable
• To save the Segment fsync is needed - expensive
• Data are not stored on disk immediately, but there is no global
lock (BASE)
PERSISTENCE - TRANSLOG
PERSISTENCE - TRANSLOG
1. New documents are stored in translog and in-memory buffer
(at this point there are not searchable)
2. At the periodically refresh process they are copied to a new
Segment that resides also in memory
– from now, document are searchable
3. When Segment Commit occurs (fsync’ed into disk), the
document is removed from translog
4. Commit and translog cleanup is called flush
periodically or when translog is too big
http://sixthjudicialdistrict.sleekup.com/wp-content/uploads/2015/08/Judge-holding-gavel.jpg
PERSISTENCE - TRANSLOG
1. Translog is configurable per index
– translog is fsynce’d every 5 sec by default
– after every insert/update/delete/index
– translog is committed after bulk-insert – worth to use
2. Can be configured as async with interval:
PUT /my_index/_settings
{
"index.translog.durability": "async",
"index.translog.sync_interval": "5s"
}
http://sixthjudicialdistrict.sleekup.com/wp-content/uploads/2015/08/Judge-holding-gavel.jpg
PER-OPERATION PERSISTENCE
http://sixthjudicialdistrict.sleekup.com/wp-content/uploads/2015/08/Judge-holding-gavel.jpg
SCALING
sharding and replication
NODES IN CLUSTER
• There is one primary shard (in replicas)
• Shard in any node can become the primary
CRUD IN CLUSTER
• Request can be handled by any node
this node will coordinate the request
• CRUD is performed on Primary Shard first and
replicated to Replicas
CONSISTENCY
• Quorum by default (majority of Shard copies)
– floor( (primary + number_of_replicas) / 2 ) + 1
– Can be:
• Quorum
• One
• All
• Defining timeout is recommened (1min by default)
– Elasticsearch will be waiting until all needed responses
appear. In the case of timeout an application should take
the decision what to do
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-wait-
for-active-shards
READ FROM CLUSTER
• Any node can receive request and coordinates it
• Results will be fetched from nodes
By default, coordinate node will choose different shard copy on every
request in order to rebalance reads (round robin)
NODES IN CLUSTER
• Every node knows where data lives (information
about shard key), so can route the request
for client this approach is transparent, can talk with
any node it want
• If client keep connections to multiple nodes, there is
no Single Point of Failure
• Round-robin, to distribute the load
DISTRIBUTED READ
QUERY PHASE / FETCH PHASE
https://www.elastic.co/guide/en/elasticsearch/guide/current/distributed-search.html
CONNECTIONS
• REST API
– connection should be kept alive (choose proper library)
• Native client
– Binary protocol
– Designed fot inter-node communication
DOCUMENT VERSIONING
• Optimistic concurency tool
• Version types
– Internal (1, 2, 3 …)
– external or external_gt
– external_gte
POST /index/type?version=TIMESTAMP
DEMO
Search in whole ElasticSearch dataset personalized for specific user
(based on neo4j graph relations)
https://neo4j.com/developer/elastic-search/
Elasticsearch - SEARCH & ANALYZE DATA IN REAL TIME
Thanks! Q&A
http://doineedbackup.com/customer-acquisition-channels-for-saas/
Thanks! Q&A
https://highspark.co/how-to-end-a-presentation/

More Related Content

PDF
阅读类 Web 应用前端技术探索
PDF
Webinar: Getting Started with Ruby and MongoDB
PDF
N hidden gems in hippo forge and experience plugins (dec17)
PDF
Djangocon 2014 angular + django
PDF
Your JavaScript Library
PDF
Build your first MongoDB App in Ruby @ StrangeLoop 2013
PDF
REST Web API with MongoDB
PDF
N hidden gems in forge (as of may '17)
阅读类 Web 应用前端技术探索
Webinar: Getting Started with Ruby and MongoDB
N hidden gems in hippo forge and experience plugins (dec17)
Djangocon 2014 angular + django
Your JavaScript Library
Build your first MongoDB App in Ruby @ StrangeLoop 2013
REST Web API with MongoDB
N hidden gems in forge (as of may '17)

What's hot (20)

PDF
Undercover Pods / WP Functions
PDF
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
PPTX
MongoDB's New Aggregation framework
KEY
MongoDB at RubyEnRails 2009
PDF
N hidden gems you didn't know hippo delivery tier and hippo (forge) could give
PDF
JSON REST API for WordPress
PDF
How to connect AngularJS to servers
PDF
Aleact
PDF
Couchdb Nosql
PDF
Php converted pdf
PDF
Building Awesome CLI apps in Go
PDF
Amazon Cloud Services and Zend Framework
PDF
Tearing the Sofa Apart: CouchDB and CouchApps from a Beginner's Perspective
PDF
Consuming RESTful services in PHP
PPTX
URI handlers
PPTX
Extending eZ Platform 2.x with Symfony and React
PPTX
WorkFlow: An Inquiry Into Productivity by Timothy Bolton
PPT
Mongo Web Apps: OSCON 2011
KEY
REST Easy - Building RESTful Services in Zend Framework
Undercover Pods / WP Functions
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
MongoDB's New Aggregation framework
MongoDB at RubyEnRails 2009
N hidden gems you didn't know hippo delivery tier and hippo (forge) could give
JSON REST API for WordPress
How to connect AngularJS to servers
Aleact
Couchdb Nosql
Php converted pdf
Building Awesome CLI apps in Go
Amazon Cloud Services and Zend Framework
Tearing the Sofa Apart: CouchDB and CouchApps from a Beginner's Perspective
Consuming RESTful services in PHP
URI handlers
Extending eZ Platform 2.x with Symfony and React
WorkFlow: An Inquiry Into Productivity by Timothy Bolton
Mongo Web Apps: OSCON 2011
REST Easy - Building RESTful Services in Zend Framework
Ad

Similar to Elasticsearch - SEARCH & ANALYZE DATA IN REAL TIME (20)

ZIP
CouchDB-Lucene
PPTX
Elasticsearch a real-time distributed search and analytics engine
PDF
Elasticsearch And Apache Lucene For Apache Spark And MLlib
PPTX
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
PPTX
Apache solr
PDF
10 Reasons to Start Your Analytics Project with PostgreSQL
PDF
REST easy with API Platform
PPTX
ElasticSearch - DevNexus Atlanta - 2014
PDF
Intro to Elasticsearch
PDF
Full Text Search In PostgreSQL
PDF
Elastic Search Training#1 (brief tutorial)-ESCC#1
PPT
How ElasticSearch lives in my DevOps life
PPTX
ElasticSearch AJUG 2013
ODP
Elastic Search
PPTX
An intro to Azure Data Lake
PDF
Introduction to Elasticsearch
PDF
Building Highly Flexible, High Performance Query Engines
PDF
MongoDB: a gentle, friendly overview
PPTX
The openCypher Project - An Open Graph Query Language
PDF
Elasto Mania
CouchDB-Lucene
Elasticsearch a real-time distributed search and analytics engine
Elasticsearch And Apache Lucene For Apache Spark And MLlib
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
Apache solr
10 Reasons to Start Your Analytics Project with PostgreSQL
REST easy with API Platform
ElasticSearch - DevNexus Atlanta - 2014
Intro to Elasticsearch
Full Text Search In PostgreSQL
Elastic Search Training#1 (brief tutorial)-ESCC#1
How ElasticSearch lives in my DevOps life
ElasticSearch AJUG 2013
Elastic Search
An intro to Azure Data Lake
Introduction to Elasticsearch
Building Highly Flexible, High Performance Query Engines
MongoDB: a gentle, friendly overview
The openCypher Project - An Open Graph Query Language
Elasto Mania
Ad

More from Piotr Pelczar (7)

PDF
Pragmatic Monolith-First, easy to decompose, clean architecture
PPTX
[BDD] Introduction to Behat (PL)
PDF
Asynchronous programming done right - Node.js
PPTX
How NOT to write in Node.js
PPTX
Liquibase - database structure versioning
PPTX
PPTX
Scalable Web Apps
Pragmatic Monolith-First, easy to decompose, clean architecture
[BDD] Introduction to Behat (PL)
Asynchronous programming done right - Node.js
How NOT to write in Node.js
Liquibase - database structure versioning
Scalable Web Apps

Recently uploaded (20)

PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
Moving the Public Sector (Government) to a Digital Adoption
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PDF
Introduction to Business Data Analytics.
PPTX
Database Infoormation System (DBIS).pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Computer network topology notes for revision
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Global journeys: estimating international migration
Data_Analytics_and_PowerBI_Presentation.pptx
Business Ppt On Nestle.pptx huunnnhhgfvu
IB Computer Science - Internal Assessment.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Business Acumen Training GuidePresentation.pptx
oil_refinery_comprehensive_20250804084928 (1).pptx
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Moving the Public Sector (Government) to a Digital Adoption
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
IBA_Chapter_11_Slides_Final_Accessible.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Clinical guidelines as a resource for EBP(1).pdf
Introduction to Business Data Analytics.
Database Infoormation System (DBIS).pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Computer network topology notes for revision
1_Introduction to advance data techniques.pptx
Global journeys: estimating international migration

Elasticsearch - SEARCH & ANALYZE DATA IN REAL TIME

  • 1. ELASTICSEARCH SEARCH & ANALYZE DATA IN REAL TIME* Piotr Pelczar • github • stackoverflow Wrocław 2017, Eurobank freeimages.com v 1.2
  • 2. AGENDA You will find out: • purpose • how data is stored and searched • features + 3rd party • architecture • usecase on production
  • 3. AGENDA You will not find out: • production ready configuration: HA, repl, sharding • monitoring • production internal bottlenecks / failure recovery • ELK stack – elasticsearch + logstash + kibana • comparison of ES i Solr/Sphinx
  • 4. PURPOSE • NoSQL • Databse for full text search • More reads then writes every document update is a creation of a new one • No transactions – BASE instead of ACID
  • 5. FULL-TEXT SEARCH • full-text search (FTS) refers to techniques of efficient search the data simillar to natural language text • search is performed using full text index • under the hood ES is powered by Apache Lucene
  • 6. FULL-TEXT SEARCH FTS is available in Oracle, MsSQL, MySQL, but... • KILLER FEATURE: ES enables to customize the proces of building the full text index • features like: – autocomplete – „Did you mean?” based Levenstein distance – indexing one field in a several ways
  • 7. ES IS POWERED BY LUCENE Features added: • clustering • sharding (horizontal scaling) • replication (copy of shards) • versioning • non-full-text indices • REST API https://pl.pinterest.com/pin/528328600014757803/
  • 8. BASE vs ACID • Atomicity • Consistency • Isolation • Durability • Basically Available • Soft state • Eventual consistency http://wallpaperswide.com/domino_effect-wallpapers.html
  • 9. TERMINOLOGY • Cluster • Node • Index (collection of docs) <-> (?) tablespace/database • Type (partition of index) <-> (?) table/collection • Document (JSON format) • Shard & Replicas https://wallpaperscraft.com/download/london_philharmonic_orchestra_scene_show_play_conductor_8925/2560x1080
  • 10. DOCUMENT-ORIENTED I SCHEMA-FREE • data is stored as documents • documents are unstructured * * by default, but there is a possibility to require strict o partly strict schema in the type definition in index • all fields are indexed in full-text by default * * this behaviour is fully configurable, data type can be changed or the field can be ignored in full-text index http://www.shximai.com/education-wallpapers.html
  • 12. RANKING FORMULA BM25 similarity function https://www.slideshare.net/Hadoop_Summit/t-435p212leauv2 1. http://ipl.cs.aueb.gr/stougiannis/bm25_2.html 2. https://en.wikipedia.org/wiki/Tf%E2%80%93idf q - query t – term d - document
  • 13. ANALYZERS Analysis - the process of converting text into tokens or terms which are added to the inverted index for searching. Analysis is performed by an analyzer. • Index time analyser • Search time analyser http://wallpaperswide.com/glasses_and_book-wallpapers.html
  • 14. ANALYZERS 1. Tokenizing a block into terms 2. Normalizing (reducing) terms into root form • Every field in document type can have own analyser • Fields can be indexed by several analysers (multi-fields) https://www.elastic.co/guide/en/elasticsearch/reference/current/multi- fields.html#_multi_fields_with_multiple_analyzers http://wallpaperswide.com/glasses_and_book-wallpapers.html
  • 15. ANALYZERS 1. Character Filters – html and entities – triming 2. Tokenizer 3. Token Filters – stopwords – Stemmer (root form) – Phonetic, n-grams – Synonim – Patten capture http://wallpaperswide.com/glasses_and_book-wallpapers.html Input Filter Tokenizer Token filter Index
  • 16. ANALYZERS Polish analyser – Stempel sudo bin/plugin install analysis-stempel http://wallpaperswide.com/glasses_and_book-wallpapers.html
  • 17. CUSTOM ANALYSERS PUT /index_name { "settings": { "analysis": { "analyzer": { "polskie_slowa": { "type": "custom", "tokenizer": "standard", "filter": ["lowercase", "stopwords_polska", "polish_stem", "asciifolding"], "char_filter": ["html_strip"] } }, "filter": { "stopwords_polska": { "type": "stop", "stopwords": ["a", "aby", ...] } } } } // ...
  • 18. TESTING ANALYSER GET /_analyze { "analyzer": "standard", "text": "Text to analyze" } { "tokens": [ { "token": "text", "start_offset": 0, "end_offset": 4, "type": "<ALPHANUM>", "position": 1 }, // ..
  • 19. INSERT PUT /website/blog/123 { "title": "My first blog entry", "text": "Just trying this out...", "date": "2014/01/01" } Custom ID - PUT POST /website/blog/ Auto ID - POST
  • 20. INSERT By default: • There is no document schema • All fields are indexed in full-text index • Type of encountered previously unknown field is determined by the first value that appeared (dynamic mapping) https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html https://studyinthestates.dhs.gov/2017/01/new-interim-final-guidance-open-for-comment-until-feb-27
  • 21. INSERT – DYNAMIC MAPPING dyanmic - true (fields out of schema are allowed) - false (new fields are just ignored) - strict (exception, when unknown field) https://studyinthestates.dhs.gov/2017/01/new-interim-final-guidance-open-for-comment-until-feb-27
  • 22. INSERT – DYNAMIC MAPPING PUT /my_index { "mappings": { "my_type": { "dynamic": "strict", "properties": { "title": { "type": "string"}, "stash": { "type": "object", "dynamic": true } } } } }
  • 23. MAPPING Data types: • Core – numeric, date, boolean, binary, string (keyword, fulltext) • Complex – array, object, nested (array of objects) – geo • Multi-fields – e.x. date as raw date and full-text value (movie title YEAR), or field with multiple analysers • Specialized – ip, completion, …
  • 24. MAPPING "mappings": { "news": { "dynamic": "strict", "properties": { "title": { "type": "string", "analyzer": "polskie_slowa", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } }, "date_published": { "type": "date", "index": "not_analyzed" }, multifield
  • 25. MAPPING "mappings": { "news": { "dynamic": "strict", "properties": { "stories": { "type": "nested", "dynamic": "strict", "properties": { "id": { "type": "long", "index": "not_analyzed" }, "title": { "type": "string", "analyzer": "polskie_slowa" } } }
  • 26. MAPPING "mappings": { "news": { "dynamic": "strict", "properties": { "media": { "type": "object", "dynamic": "strict", "properties": { "gallery_has": { "type": "boolean", "index": "not_analyzed" }, "video_has": { "type": "boolean", "index": "not_analyzed" }, "poll_has": { "type": "boolean", "index": "not_analyzed" },
  • 27. GET GET /website/blog/123 { "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 1, "found" : true, "_source" : { "title": "My first blog entry", "text": "Just trying this out...", "date": "2014/01/01" } } Status code: 200, 404
  • 29. UPDATE PUT /website/blog/123 { "title": "My first blog entry", "text": "Just trying this out...", "date": "2014/01/01" } POST /website/blog/123/_update { "title": "My first blog entry„ }
  • 30. UPDATE – IMMUTABLE DOCS • Documents are immutable, because Lucene segments are immutable • The new version of documents are created • Old version is marked in .del file • Previous version is still searchable, but is removed from search result in the runtime until cleanup process https://studyinthestates.dhs.gov/2017/01/new-interim-final-guidance-open-for-comment-until-feb-27
  • 31. URI SEARCH GET /index/_search?q=user:kimchy GET /index/type1,type2/_search?q=user:kimchy GET /index1,index2/type/_search?q=user:kimchy GET /_all/type/_search?q=user:kimchy GET /_search?q=user:kimchy
  • 32. URI SEARCH { "timed_out": false, "took": 62, "hits":{ "total" : 1, "max_score": 1.3862944, "hits" : [ { "_index" : "twitter", "_type" : "tweet", "_id" : "0", "_score": 1.3862944, "_source" : { "user" : "kimchy", "date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch", "likes": 0 }
  • 33. TERM QUERY GET /index/type/_search { "query" : { "term" : { "user" : "kimchy" } } }
  • 34. SEARCH Search segments: • must-match • should-match (scoring) • fuzzy-query (Levenstein desitnation) • filter (without scoring, very fast) • limit/offset
  • 35. TERM QUERY & BOOST "query": { "bool": { "should" : [ { "match": { "title": { "query": "myśliwy", "boost": 1 } }, ], "filter": { "and" : [ { "term": { "media.gallery_has": false } } ] }
  • 36. HIGHLIGHTING GET /_search { "query" : { "match": { "content": "kimchy" } }, "highlight" : { "fields" : { "content" : {} } } }
  • 37. SUGGESTING POST music/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "nir", "completion" : { "field" : "suggest" } } } }
  • 38. SUGGESTING "suggest": { "song-suggest" : [ { "text" : "nir", "offset" : 0, "length" : 3, "options" : [ { "text" : "Nirvana", "_index": "music", "_type": "song", "_id": "1", "_score": 1.0, "_source": { "suggest": ["Nevermind", "Nirvana"] } } ]
  • 39. SCORE FUNCTIONS • Weight • Field Value factor • Decay functions https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html
  • 40. SCORE FUNCTIONS - DECAY Model functions: • Gauss • Exp • Linear • Multivalue – 2d – 3d – n …
  • 41. SCORE FUNCTIONS - DECAY "function_score": { "query": { ... }, "score_mode": "multiply", // how functions are compared "boost_mode": "multiply", // how functions has impact to original score "functions": { "date_published": { "origin": " ... " "offset": " ... " "scale": " ... " } }
  • 43. REINDEX POST _reindex { "source": { "index": "twitter" }, "dest": { "index": "new_twitter" } } Every index change needs index rebuild (reindex) : • Change data types, schema • Analyser modification • Shard key, numer of shards (no way to rebalance data)
  • 44. REINDEX POST _reindex { "source": { "index": "twitter", "type": "tweet", "query": { "term": { "user": "kimchy" } } }, "dest": { "index": "new_twitter" } } 2x more space is needed, but there is the possibility to do query width limit and offset and delete data from old index in the meantime. Application have to query both indices/aliases during reindex process and filter duplicates in the runtime.
  • 45. Index Aliases and Zero Downtime POST /_aliases { "actions" : [ { "remove" : { "index" : "news_v1", "alias" : "news_view" } }, { "add" : { "index" : "news_v2", "alias" : "news_view" } } ] } https://www.elastic.co/guide/en/ elasticsearch/guide/current/index-aliases.html
  • 46. * REAL TIME, NEAR REALTIME (NRT) This means is there is a slight latency (normally one second) from the time you index a document until the time it becomes searchable. http://all-free-download.com/wallpapers/animals/horse_racing_wallpaper_horses_animals_wallpaper_382.html
  • 47. NEAR REALTIME (NRT) 1. Data have to be analysed 2. There are caveats with Lucene segments
  • 48. NRT i BASE In the system there is no global-lock -> append-only, immutable -> no transactions -> >> response, availability => PROFIT! -> data could be not visible immediately Stale data, but any data... http://wallpaperswide.com/domino_effect-wallpapers.html
  • 49. NEAR REALTIME (NRT) • New index is created in new Lucene Segments periodicaly • Creation new Lucene Segments are called refresh • 1 sec by default - NRT • INSERT -> GET can respond 404.
  • 50. NEAR REALTIME (NRT) • Lucene allows to open a new Segment and search in it without Commit • Commit makes Lucene Segment immutable • Document is always added to a new Segment that temporarily resides in memory (file system cache) and are searchable • To save the Segment fsync is needed - expensive • Data are not stored on disk immediately, but there is no global lock (BASE)
  • 52. PERSISTENCE - TRANSLOG 1. New documents are stored in translog and in-memory buffer (at this point there are not searchable) 2. At the periodically refresh process they are copied to a new Segment that resides also in memory – from now, document are searchable 3. When Segment Commit occurs (fsync’ed into disk), the document is removed from translog 4. Commit and translog cleanup is called flush periodically or when translog is too big http://sixthjudicialdistrict.sleekup.com/wp-content/uploads/2015/08/Judge-holding-gavel.jpg
  • 53. PERSISTENCE - TRANSLOG 1. Translog is configurable per index – translog is fsynce’d every 5 sec by default – after every insert/update/delete/index – translog is committed after bulk-insert – worth to use 2. Can be configured as async with interval: PUT /my_index/_settings { "index.translog.durability": "async", "index.translog.sync_interval": "5s" } http://sixthjudicialdistrict.sleekup.com/wp-content/uploads/2015/08/Judge-holding-gavel.jpg
  • 56. NODES IN CLUSTER • There is one primary shard (in replicas) • Shard in any node can become the primary
  • 57. CRUD IN CLUSTER • Request can be handled by any node this node will coordinate the request • CRUD is performed on Primary Shard first and replicated to Replicas
  • 58. CONSISTENCY • Quorum by default (majority of Shard copies) – floor( (primary + number_of_replicas) / 2 ) + 1 – Can be: • Quorum • One • All • Defining timeout is recommened (1min by default) – Elasticsearch will be waiting until all needed responses appear. In the case of timeout an application should take the decision what to do https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-wait- for-active-shards
  • 59. READ FROM CLUSTER • Any node can receive request and coordinates it • Results will be fetched from nodes By default, coordinate node will choose different shard copy on every request in order to rebalance reads (round robin)
  • 60. NODES IN CLUSTER • Every node knows where data lives (information about shard key), so can route the request for client this approach is transparent, can talk with any node it want • If client keep connections to multiple nodes, there is no Single Point of Failure • Round-robin, to distribute the load
  • 61. DISTRIBUTED READ QUERY PHASE / FETCH PHASE https://www.elastic.co/guide/en/elasticsearch/guide/current/distributed-search.html
  • 62. CONNECTIONS • REST API – connection should be kept alive (choose proper library) • Native client – Binary protocol – Designed fot inter-node communication
  • 63. DOCUMENT VERSIONING • Optimistic concurency tool • Version types – Internal (1, 2, 3 …) – external or external_gt – external_gte POST /index/type?version=TIMESTAMP
  • 64. DEMO
  • 65. Search in whole ElasticSearch dataset personalized for specific user (based on neo4j graph relations) https://neo4j.com/developer/elastic-search/