Slide 1

Slide 1 text

Elastic
 An Elasticsearch Client for Go Oliver Eilhard
 Meplato GmbH
 2015-02-05

Slide 2

Slide 2 text

Search Meetup Munich _ 5th Feb 2015 Who is this guy? • Programming addict, consultant, trainer • Go, Ruby, C#/.NET • E-Procurement • Past: wallmedien AG, CTO • Currently: Meplato GmbH, CTO

Slide 3

Slide 3 text

Why is this guy standing between me and Shay^H beer?

Slide 4

Slide 4 text

Search Meetup Munich _ 5th Feb 2015 Agenda • Show off Elastic:
 Our Elasticsearch client for Go • Using Elasticsearch, Elastic and Go for a fun project

Slide 5

Slide 5 text

Search Meetup Munich _ 5th Feb 2015 What is Go? Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. • https://www.golang.org/ • Robert Griesemer, Rob Pike, Ken Thompson, Brad Fitzpatrick etc. • Open sourced on Nov 10, 2009 • Statically typed, compiled language,
 garbage collected

Slide 6

Slide 6 text

Search Meetup Munich _ 5th Feb 2015 Pros/Cons of Go • Compiles crazy fast • Simple: 25 keywords • Good at concurrency: Channels, light-weight processes (goroutines) • Novel ideas: Interfaces are implicit, type embedding • Creates a static binary: No dependency headaches • Very good at cross-compiling • Solid standard library • Integrated package management system (without versioning) • Opinionated: No generics, no type inheritance, no method overloading, no operators, no exceptions, no pointer arithmetic.

Slide 7

Slide 7 text

package  main   //  Import  the  fmt  package  from  the  standard  library   import  "fmt"   //  Program  starts  here   func  main()  {          fmt.Println("Hello")   }   Hello World in Go

Slide 8

Slide 8 text

func  worker(id  int,  jobs  <-­‐chan  int)  {          for  job  :=  range  jobs  {                  fmt.Printf("Worker  %d  processing  job  %d\n",  id,  job)                  time.Sleep(time.Duration(rand.Int63n(1000))  *  time.Millisecond)          }   }   func  main()  {          //  Create  buffered  channel  with  up  to  100  entries          jobs  :=  make(chan  int,  100)          //  Start  3  workers          for  w  :=  1;  w  <=  3;  w++  {                  go  worker(w,  jobs)          }          //  Send  data  down  the  channel  to  the  workers          for  j  :=  1;  j  <=  1000;  j++  {                  jobs  <-­‐  j          }          close(jobs)   } Concurrency in Go

Slide 9

Slide 9 text

Search Meetup Munich _ 5th Feb 2015 What I like about Go • Very intuitive and clean • Productive within days • Rob Pike and Ken Thompson obviously know a bit or two about programming • They have a mountain of experience, good taste, and solid opinions • They favor simplicity, minimalism, and clean code Recently on Hacker News: “I'm more surprised that the Go 1.4 compiler is written in C. Writing a compiler in C sounds very unpleasant compared to a language like OCaml. Does anyone know why they made this choice for the initial design?“ — “Ken Thompson wrote it. He's pretty good at C.“ Give Go a try e.g. by visiting the Go Tour at https://tour.golang.org/

Slide 10

Slide 10 text

Search Meetup Munich _ 5th Feb 2015 Elasticsearch at Meplato • Product catalogs • Analytics

Slide 11

Slide 11 text

Search Meetup Munich _ 5th Feb 2015 Elastic No solid Elasticsearch client for Go at the time of writing our code. So we wrote our own. We released it as open source last year. https://olivere.github.com/elastic (Home)
 https://github.com/olivere/elastic (Source)

Slide 12

Slide 12 text

package  main   import  (          "fmt"          "net/http"          "github.com/olivere/elastic"   )   func  main()  {          //  Get  a  client          client,  _  :=  elastic.NewClient(http.DefaultClient)          //  Get  the  version  for  some  ES  server          version,  _  :=  client.ElasticsearchVersion("http://127.0.0.1:9200")          fmt.Printf("You're  running  Elasticsearch  version  %s",  version)   } Example: Determine Elasticsearch version

Slide 13

Slide 13 text

Search Meetup Munich _ 5th Feb 2015 Elastic: Central concepts • The client exposes services, e.g. CreateIndex or Search • Services follow the "builder pattern" • Services have a Do method to execute • Follow the Java client API

Slide 14

Slide 14 text

Search Meetup Munich _ 5th Feb 2015 Elastic: Features • Focus on search/data management, not on cluster/administration (for now) • APIs: 14 of 23 implemented • Query DSL: 26 of 39 • Filters: 20 of 27 • Facets: 9 of 9 • Aggregations: 28 of 29

Slide 15

Slide 15 text

//  If  "test"  index  exists,  delete  it   exists,  err  :=  client.IndexExists("test").Do()   if  err  !=  nil  {          panic(err)   }   if  exists  {          if  _,  err  :=  client.DeleteIndex("test").Do();  err  !=  nil  {                  panic(err)          }          fmt.Println("Index  deleted")   }   //  Create  a  new  index  named  "test"   mapping  :=  `{"settings":{"number_of_shards":1,"number_of_replicas":0}}`   _,  err  =  client.CreateIndex("test").Body(mapping).Do()   if  err  !=  nil  {        panic(err)   }   fmt.Println("Index  created") Example: Creating and deleting indices

Slide 16

Slide 16 text

type  Tweet  struct  {          User        string  `json:"user"`          Message  string  `json:"message"`   }   …   //  Create  a  new  tweet   tweet  :=  Tweet{          User:        "telexico",          Message:  "At  the  Elasticsearch  meetup,  having  fun  with  the  good  people  here.",   }   //  Add  document  to  Elasticsearch   _,  err  =  client.Index().
                      Index("test").
                      Type("tweet").
                      Id("1").
                      BodyJson(tweet).
                      Do()   if  err  !=  nil  {        panic(err)   }   fmt.Println("Document  added") Example: Adding a document

Slide 17

Slide 17 text

//  Get  document  of  type  "tweet"  with  id="1"  from  index  "test"   doc,  err  :=  client.Get().
                            Index("test").
                            Type("tweet").
                            Id("1").
                            Do()   if  err  !=  nil  {        panic(err)   }   //  Unmarshal  JSON   var  tweet  Tweet   if  err  :=  json.Unmarshal(*doc.Source,  &tweet);  err  !=  nil  {        panic(err)   }   fmt.Printf("Tweet  %s  by  %s:\n%q\n",  
                        doc.Id,  tweet.User,  tweet.Message) Example: Retrieving a document by ID

Slide 18

Slide 18 text

//  Search  for  documents  of  user  "telexico"   termQuery  :=  elastic.NewTermQuery("user",  "telexico")   res,  err  :=  client.Search().
                            Index("test").
                            Query(termQuery).
                            Do()   if  err  !=  nil  {          panic(err)   }   //  Print  results   fmt.Printf("%d  hits:\n\n",  res.Hits.TotalHits)   for  _,  doc  :=  range  res.Hits.Hits  {          var  tweet  Tweet          json.Unmarshal(*doc.Source,  &tweet)          fmt.Printf("@%s:  %s\n",  tweet.User,  tweet.Message)   } Example: Searching by Term Query

Slide 19

Slide 19 text

Search Meetup Munich _ 5th Feb 2015 Let’s do something different How do you learn stuff best?
 By applying it to a real-world problem. So we went here…

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Search Meetup Munich _ 5th Feb 2015 Meplato Racing Racing is our passion. We took part in the complete 2014 VLN season at the Nordschleife. Maybe we can apply technology here, too.
 
 
 VLN: http://www.vln.de

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Search Meetup Munich _ 5th Feb 2015 Problem identified We were too slow: ~20 seconds per lap.
 So where exactly did we lose time? What do good engineers do? 1. Gather data
 2. Analyze data

Slide 24

Slide 24 text

Search Meetup Munich _ 5th Feb 2015 GPS Tracking • Cars have GPS on board • Cars also offer tons of other data from gearbox, clutch, brakes etc. • We only need: • Latitude/Longitude • Speed • Steering wheel position

Slide 25

Slide 25 text

Search Meetup Munich _ 5th Feb 2015 GPS Tracking Ideas • Downloading data at pitstops? • Too slow • We can track only our own car

Slide 26

Slide 26 text

Search Meetup Munich _ 5th Feb 2015 GPS Tracking Ideas • „Racing App“ by GPSoverIP GmbH • Provides live car tracking over the Internet • Let’s write a proxy and record data into Elasticsearch
 Racing App: https://itunes.apple.com/de/app/racing-app/id444591012?mt=8

Slide 27

Slide 27 text

{      "_index"  :  "racetrack-­‐2014-­‐08-­‐02",      "_type"  :  "record",      "_id"  :  "bXyMvt-­‐-­‐RlGG43n3zJP8ug",      "_score"  :  1.0,      "_source":{              "id":"nbr24@216654@2014-­‐08-­‐02T11:09:27.040151138Z",              "track":"nbr24",              "device":"216654",              "name":"Opel  Astra  OPC  CUP",              "nr":"357",              "class":"CUP1",              "location":"50.348095,6.971377",              "direction":237,              "speed":234.3,              "ts":"2014-­‐08-­‐02T11:09:23Z",              "created":"2014-­‐08-­‐02T11:09:27.040157548Z"      }   } GPS Tracking

Slide 28

Slide 28 text

Search Meetup Munich _ 5th Feb 2015 How do we track sector times? • GeoJSON for map and sectors • http://geojson.io/

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Search Meetup Munich _ 5th Feb 2015 Analyze data with Elasticsearch • Run a program to track sector entry and exit times for the cars • Elasticsearch has strong Geo query capabilities that make this super simple • Make a nice PDF for the non-geeks

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Search Meetup Munich _ 5th Feb 2015 Analyze the race afterwards • We are great fans of learning by visualization • So we used D3.js to run the race again, fast-forward

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Search Meetup Munich _ 5th Feb 2015 What we learned • Elasticsearch is a realtime document store • Elasticsearch is crazy fast • Elasticsearch has exhaustive query capabilities • GeoJSON for geo data • D3.js for visualization • How to run the Nordschleife 
 in sub-9:30m

Slide 35

Slide 35 text

Thank you
 
 Oliver Eilhard
 Meplato GmbH
 [email protected]
 https://www.meplato.com/
 @telexico