准备
go包:github.com/olivere/elastic/v7
elasticsearch权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
Kibana:1、https://www.elastic.co/guide/en/kibana/current/getting-started.html
2、https://www.cnblogs.com/yiwangzhibujian/p/7137546.html
如果用浏览器访问Kibana提示“Kibana server is not ready yet”也有可能是360拦截了,退出360即可。。。
windows命令行启动
启动es
bin> .\elasticsearch.bat
启动kibana
bin> .\kibana.bat
使用go操作
//生产100条数据
type Tweet struct {
User string
Message string
}
func main() {
client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL("http://127.0.0.1:9200/"))
if err != nil{
fmt.Println("connect es error:", err)
return
}
fmt.Println("conn es succ")
for i := 0; i<100; i++ {
tweet := Tweet{"olivere", "Take Six"}
_, err = client.Index().
Index("twitter").
Type("tweet").
Id(fmt.Sprintf("%d",i)).
BodyJson(tweet).
Do(context.Background())
if err != nil {
panic(err)
return
}
}
fmt.Println("insert succ")
}
浏览器中通过Kibana查看elasticsearch中的数据 ,输入框中输入筛选字符快速查找
详细
创建client
client, err := elastic.NewClient(
elastic.SetURL("http://10.0.1.1:9200", "http://10.0.1.2:9200"), //不设置的话默认到127.0.0.1:9200(本机)
elastic.SetSniff(false), //不设置嗅探器,嗅探器用于自动发现Elasticsearch的节点,并设置为restClient实例
elastic.SetHealthcheckInterval(10*time.Second), //每10秒检查一次集群健康状态
elastic.SetMaxRetries(5), //请求失败后,最多再尝试5次
elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)), //输出错误信息到stderr
elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags))) //输出通知信息到stdout
查看es的版本号
esversion, err := client.ElasticsearchVersion("http://127.0.0.1:9200")
查看节点的健康状况
res, err := client.ClusterHealth().Index("twitter").Do(context.Background()) //在单机模式下健康为黄色,因为此时没有另一台机器存放数据副本
节点健康状低于指定颜色则等待
res, err := client.ClusterHealth().WaitForStatus("green").Timeout("15s").Do(context.Background())
if err != nil {
//panic(err)
fmt.Println(err)
}
if res.TimedOut {
fmt.Printf("time out waiting for cluster status %q\n", "green")
} else {
fmt.Printf("cluster status is %q\n", res.Status)
}
确认某index是否存在
exists, err := client.IndexExists("twitter").Do(context.Background())
if exists {
// ...
}
创建index
createIndex, err := client.CreateIndex("twitter").Do(context.Background()) //如果index已存在会报panic
if !createIndex.Acknowledged {
// 不成功...
}
...
删除index
deleteIndex, err := client.DeleteIndex("twitter").Do(context.Background()) //删除不存在的index会报panic
if !deleteIndex.Acknowledged {
//不成功...
}
向index插入document
//用struct格式
type Tweet struct {
User string `json:"user"`
Message string `json:"message"` //后缀json的话,会存入对应小写的键名
}
tweet1 := Tweet{User: "olivere", Message: "Take Five"}
put1, err := client.Index().Index("twitter").Id("1").BodyJson(tweet1).Do(context.Background())
//用json格式的string
tweet2 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
put2, err := client.Index().Index("twitter").Id("2").BodyString(tweet2).Do(context.Background())
取值
//按ID取值
get, err := client.Get().Index("twitter").Id("5").Do(context.Background())
if err != nil {
switch {
case elastic.IsNotFound(err):
panic(fmt.Sprintf("Document not found: %v", err))
case elastic.IsTimeout(err):
panic(fmt.Sprintf("Timeout retrieving document: %v", err))
case elastic.IsConnErr(err):
panic(fmt.Sprintf("Connection problem: %v", err))
default:
panic(err)
}
}
...
搜索
可以交给kibana。。。