Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elasticsearch for SQL Users

Elasticsearch for SQL Users

Most developers are familiar with relational databases and its query language, SQL. We know how to model our data so it can fit the relational paradigm and we know how to query this relational data using SQL. Elasticsearch is a document store with its own query DSL. In this talk we will look at several well-understood concepts and SQL queries from the relational paradigm and map these to their Elasticsearch equivalents. If you are someone with a solid grasp of the relational world and looking to map your mental model to the document-oriented search world, this talk is for you!

Philipp Krenn

October 04, 2016
Tweet

More Decks by Philipp Krenn

Other Decks in Programming

Transcript

  1. CREATE TABLE IF NOT EXISTS emails ( sender VARCHAR(255) NOT

    NULL, recipients TEXT, cc TEXT, bcc TEXT, subject VARCHAR(1024), body MEDIUMTEXT, datetime DATETIME ); CREATE INDEX emails_sender ON emails(sender); CREATE FULLTEXT INDEX emails_subject ON emails(subject); CREATE FULLTEXT INDEX emails_body ON emails(body);
  2. PUT /communication { "mappings": { "email": { "properties": { "sender":

    { "type": "keyword" }, "recipients": { "type": "keyword" }, "cc": { "type": "keyword" }, "bcc": { "type": "keyword" }, "subject": { "type": "text", "analyzer": "english" }, "body": { "type": "text", "analyzer": "english" } } } } }
  3. Let's Add Some Data POST /communication/email { "sender": "[email protected]", "recipients":

    [ "[email protected]" ], "cc": [], "subject": "Elasticsearch is pretty cool", "body": "Hey Philipp, this is great stuff. Check it out!" }
  4. Let's Add Some Data POST /communication/email { "sender": "[email protected]", "recipients":

    [ "[email protected]" ], "cc": [ "[email protected]" ], "subject": "Thanks for creating Elasticsearch", "body": "David pointed me to your project. It's awesome" }
  5. Let's Add Some Data POST /communication/email { "sender": "[email protected]", "recipients":

    [ "[email protected]" ], "cc": [], "subject": "We should hire Philipp", "body": "He is really into the project and will do great things." }
  6. Analyzed Text PUT /cities/city/1 { "city": "Sandusky", "population": 25340 }

    PUT /cities/city/2 { "city": "New Albany", "population": 8829 } PUT /cities/city/3 { "city": "New York", "population": 8406000 }
  7. Not Analyzed Text DELETE /cities PUT /cities { "mappings": {

    "city": { "properties": { "city": { "type": "keyword" } } } } }
  8. Search for the Phrase great stuff POST /communication/_search { "query":

    { "match_phrase": { "body": "great stuff" } } }
  9. Boosting Positive (>1) or negative (<1) POST /communication/_search { "query":

    { "multi_match": { "query": "Philipp", "fields": [ "subject^3", "body" ] } } }
  10. Application-Side Joins PUT /blog/author/1 { "name": "Philipp", "bio": "..." }

    PUT /blog/post/1 { "author_id": 1, "title": "...", "body": "..." } PUT /blog/post/2 { "author_id": 1, "title": "...", "body": "..." }
  11. Application-Side Joins POST /blog/author/_search { "query": { "match": { "name":

    "Philipp" } } } POST /blog/post/_search { "query": { "match": { "author_id": <each id from query 1 result> } } }
  12. Data Denormalization PUT /blog/post/1 { "author_name": "Philipp", "title": "...", "body":

    "..." } PUT /blog/post/2 { "author_name": "Philipp", "title": "...", "body": "..." }
  13. Nested Objects PUT /blog/author/1 { "name": "Philipp", "bio": "...", "blog_posts":

    [ { "title": "...", "body": "..." }, { "title": "...", "body": "..." }, { "title": "...", "body": "..." } ] }
  14. Parent-Child Documents PUT /blog/author/1 { "name": "Philipp", "bio": "..." }

    PUT /blog/post/1?parent=1 { "title": "...", "body": "..." } PUT /blog/post/2?parent=1 { "title": "...", "body": "..." }
  15. Parent-Child Documents POST /blog/post/_search { "query": { "has_parent": { "type":

    "author", "query": { "match": { "name": "Philipp" } } }
  16. Can I use it as a primary datastore? It depends

    https://www.elastic.co/guide/en/elasticsearch/ resiliency/current/index.html