1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package main
import ( "context" "fmt" "github.com/olivere/elastic" )
var client *elastic.Client
var host = "http://192.168.43.176:9200"
type Employee struct { FirstName string `json:"first_name"` LastName string `json:"last_name"` Age int `json:"age"` About string `json:"about"` Interests []string `json:"interests"` }
func init() { var err error client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host)) if err != nil { panic(err) } _,_,err = client.Ping(host).Do(context.Background()) if err != nil { panic(err) }
_,err = client.ElasticsearchVersion(host) if err != nil { panic(err) } }
func create() {
e1 := Employee{"Jane", "Smith", 32, "I like to collect rock albums", []string{"music"}} put1, err := client.Index(). Index("megacorp"). Type("employee"). Id("1"). BodyJson(e1). Do(context.Background()) if err != nil { panic(err) } fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put1.Id, put1.Index, put1.Type)
e2 := `{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests":["sports","music"]}` put2, err := client.Index(). Index("megacorp"). Type("employee"). Id("2"). BodyJson(e2). Do(context.Background()) if err != nil { panic(err) } fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put2.Id, put2.Index, put2.Type)
e3 := `{"first_name":"Douglas","last_name":"Fir","age":35,"about":"I like to build cabinets","interests":["forestry"]}` put3, err := client.Index(). Index("megacorp"). Type("employee"). Id("3"). BodyJson(e3). Do(context.Background()) if err != nil { panic(err) } fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put3.Id, put3.Index, put3.Type) }
func main() { create() }
|