今日我们将要介绍ES官方提供go-elasticsearch
客户端的基本操作。
用go get
安装 go-elasticsearch
go get github.com/elastic/go-elasticsearch/v8
建立elasticsearch客户端的连线设定
package main
import (
"log"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
)
var es *elasticsearch.Client
func setElkClient() {
var err error
cfg := elasticsearch.Config{
Addresses: []string{"http://127.0.0.1:9200"},
}
es, err = elasticsearch.NewClient(cfg)
if err != nil {
panic(err) // 连线失败
}
}
func main() {
setElkClient()
fmt.Println(es.Info())
}
{
"name" : "CY-HUANG",
"cluster_name" : "elasticsearch_huang",
"cluster_uuid" : "05loK4i4TVmTdRBUHEpi5g",
"version" : {
"number" : "7.14.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "66b55ebfa59c92c15db3f69a335d500018b3331e",
"build_date" : "2021-08-26T09:01:05.390870785Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
建立索引
func createIndex() {
req := esapi.IndicesCreateRequest{
Index: "test_index",
}
res, err := req.Do(context.Background(), es)
if err != nil {
panic(err)
}
defer res.Body.Close()
log.Println(res)
}
[200 OK] {"acknowledged":true,"shards_acknowledged":true,"index":"test_index"}
新增资料,如果索引不存在,则会自动建立索引。
func IndexRequest() {
req := esapi.IndexRequest{
Index: "test_index",
Body: strings.NewReader(`{"title":"go es index test", "Content": "elasticsearch client"}`),
}
res, err := req.Do(context.Background(), es)
if err != nil {
panic(err)
}
defer res.Body.Close()
log.Println(res)
}
[201 Created] {"_index":"test_index","_type":"_doc","_id":"6NwZS3wB63oJvfCKy9ZX","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
使用Query DSL
语法查询资料
func searchRequest() {
req := esapi.SearchRequest{
Index: []string{"test_index"},
Body: strings.NewReader(`{"from":0,"query":{"bool":{"must":[{"match":{"title":{"operator":"and","query":"go es index test"}}}]}},"size":20}`),
}
res, err := req.Do(context.Background(), es)
if err != nil {
panic(err)
}
defer res.Body.Close()
log.Println(res)
}
[200 OK] {"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.1507283,"hits":[{"_index":"test_index","_type":"_doc","_id":"6NwZS3wB63oJvfCKy9ZX","_score":1.1507283,"_source":{"title":"go es index test", "Content": "elasticsearch client"}}]}}
删除索引
func deleteIndex() {
req := esapi.IndicesDeleteRequest{
Index: []string{"test_index"},
}
res, err := req.Do(context.Background(), es)
if err != nil {
panic(err)
}
defer res.Body.Close()
log.Println(res)
}
[200 OK] {"acknowledged":true}
如需了解更多go-elasticsearch讯息请参阅
>>: Day 20 : 案例分享(6.3) 人事、差勤与薪资 - 线上请休假及签到退
tags: 铁人赛 AWS Outposts EKS Kubernetes 简述 EKS 说明 Am...
大家好,我是羊小咩 这章来谈谈混合加密系统(hybrid cryptosystem) 现今大多传送...
今天是最後一天了,每天看这本书《听说做完380个实例,就能成为.NET Core大内高手》,真的里面...
当你的storyboard里有太多元件或是你用了太多的storyboard可能会使你的Xcode卡顿...
前言 如果有接触过 Redux 的话,应该会对这个 hook 有亲切感。如果是像我一样没有接触的话也...