package main

import (
	"context"
	"fmt"

	"github.com/olivere/elastic/v7"
)

type Person struct {
	Name    string `json:"name"`
	Age     int    `json:"age"`
	Married bool   `json:"married"`
}

func main() {
	client, err := elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"), elastic.SetBasicAuth("elastic", "elastic"))
	if err != nil {
		// Handle error
		panic(err)
	}
	fmt.Println("connect to es success")
	person := Person{Name: "talen", Age: 39, Married: true}
	result, err := client.Index().
		Index("user-test").
		BodyJson(person).
		Do(context.Background())
	if err != nil {
		// Handle error
		panic(err)
	}
	fmt.Printf("user-test %s to index %s, type %s\n", result.Id, result.Index, result.Type)
}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐