elastic client
We use a third-party library https://github.com/olivere/elastic to connect to ES and perform operations.
Pay attention to download the client of the same version as your ES. For example, the ES we use here is version 7.2.1, so the client we downloaded should also correspond to github.com/olivere/elastic/v7.
rely:
require (
github.com/olivere/elastic/v7 v7.0.4
)
Simple example:
package main
import (
"context"
"fmt"
"github.com/olivere/elastic/v7"
)
// Elasticsearch demo
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Married bool `json:"married"`
}
func main() {
client, err := elastic.NewClient(elastic.SetURL("http://192.168.1.7:9200"))
if err != nil {
// Handle error
panic(err)
}
fmt.Println("connect to es success")
p1 := Person{
Name: "rion", Age: 22, Married: false}
put1, err := client.Index().
Index("user").
BodyJson(p1).
Do(context.Background())
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Indexed user %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
}