SHARE
TWEET

golang elasticsearch read

diegogaulke Feb 1st, 2019 (edited) 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "context"
  5.     "encoding/json"
  6.     "fmt"
  7.     "log"
  8.     "os"
  9.  
  10.     "github.com/olivere/elastic"
  11. )
  12.  
  13. func main() {
  14.     args := os.Args
  15.  
  16.     if len(args) < 2 {
  17.         log.Fatal("State not informed")
  18.     }
  19.  
  20.     // check if all elasticsearch env vars were exported
  21.     url := os.Getenv("ELASTIC_URL")
  22.     index := os.Getenv("ELASTIC_INDEX")
  23.     _type := os.Getenv("ELASTIC_TYPE")
  24.  
  25.     if url == "" || index == "" || _type == "" {
  26.         log.Fatal("Please set all elastic search env vars.")
  27.     }
  28.  
  29.     // get elastic search client
  30.     client, _ := elastic.NewClient()
  31.     ctx := context.Background()
  32.  
  33.     // search for state
  34.     state, _ := client.Get().
  35.         Index(index).
  36.         Type(_type).
  37.         Id(args[1]).
  38.         Do(ctx)
  39.  
  40.     j, _ := json.Marshal(state)
  41.  
  42.     fmt.Println(string(j))
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top