您当前的位置:学无止境 > Go简单操作Elasticsearch网站首页学无止境
Go简单操作Elasticsearch
发布时间:2021-03-16 16:51:16编辑:sanqing查看次数:769
下载(注意下载与你的ES相同版本的client,例如我们这里使用的ES是7.2.1的版本,那么我们下载的client也要与之对应为github.com/olivere/elastic/v7)
go get github.com/olivere/elastic/v7
main.go
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://127.0.0.1:9200"))
if err != nil {
// Handle error
panic(err)
}
fmt.Println("connect to es success")
p1 := Person{Name: "sanqing", Age: 18, Married: false}
put1, err := client.Index().
Index("www").
Type("user").
BodyJson(p1).
Do(context.Background())
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Indexed user %s to index %s, type %sn", put1.Id, put1.Index, put1.Type)
}启动 Elasticsearch 服务。参考:https://lovesanqing.com/knowledge-179.html
关键字词:Go,Elasticsearch,搜索,sphinx
下一篇:kafka的安装和简单使用
评论:
