弹性公式:适用于Go语言的Elasticsearch客户端「go-elasticsearch (v0.0.0)」
中国语言的提交选项:
公式Go语言使用的elasticsearch客户端。
自2019年初开始,Elastic官方开始开发适用于Go语言的Elasticsearch客户端,该客户端被称为go-elasticsearch。截至本文撰写(2019年3月31日),该客户端仍处于开发中,版本号为v0.0.0。虽然目前还无法确定将会实现到哪个程度,但在查看端点时,可以看到已经实现了用于向各种端点发送请求的代码,尽管暂时未发现创建搜索查询的实现,但仍对其进行了简单尝试。
尝试试一试。
首先按照README.md上所写的尝试一下。
package main
import (
"log"
"github.com/elastic/go-elasticsearch"
)
func main() {
es, _ := elasticsearch.NewDefaultClient()
log.Println(es.Info())
}
[200 OK] {
"name" : "pf0bv5Y",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "kwg4riSOQUWDGUSYvXAcTw",
"version" : {
"number" : "6.3.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "424e937",
"build_date" : "2018-06-11T23:38:03.357887Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
如果没有任何设置需要进行,那么不需要做任何设置。
es, _ := elasticsearch.NewDefaultClient()
log.Println(es.Info())
只需如此即可对Endpoint进行请求。
如果不附带参数,则可以以相同方式实现Get系列API的代码。
对_cat/indices API的请求。
es, _ := elasticsearch.NewDefaultClient()
log.Println(es.Cat.Indices())
green open test_index_20190330 PzulHkoxS-CF2xDojnOrPQ 5 1 513 0 770.9kb 770.9kb
green open test_index_20190331 BJi44UEjTxyNXCDL3lAhJw 5 1 487 0 727.8kb 727.8kb
<nil>
如果需要添加参数的话
我要确认 cat/indices 请求部分的实现。go-elasticsearch 的 api.cat.indices 文件。
type CatIndices func(o ...func(*CatIndicesRequest)) (*Response, error)
func (f CatIndices) WithContext(v context.Context) func(*CatIndicesRequest) {・・・}
func (f CatIndices) WithIndex(v ...string) func(*CatIndicesRequest) {・・・}
func (f CatIndices) WithV(v bool) func(*CatIndicesRequest) {・・・}
type CatIndicesRequest struct {
Index []string
Bytes string
Format string
H []string
Health string
Help *bool
Local *bool
MasterTimeout time.Duration
Pri *bool
S []string
V *bool
Pretty bool
Human bool
ErrorTrace bool
FilterPath []string
ctx context.Context
}
每个端点都有一个请求结构体和一个用于参数设置的函数。
查询的指定方式似乎有以下两种模式。
catIndices := es.Cat.Indices
log.Println(catIndices(catIndices.WithV(true)))
req := esapi.CatIndicesRequest{
V: esapi.BoolPtr(true),
}
ctx := context.Background()
log.Println(req.Do(ctx, es))
在README中写道的是后者。
参考:
– 猫咪接口
– go-elasticsearch 猫咪索引 API
印象
只要不需要进行复杂查询的部分,每个端点已经准备好了相应的请求结构体,可以使用。(但仍在开发中)
希望能够轻松实现搜索、批量操作等复杂查询。
作为个人,我迫切地期待着这个官方客户端的正式发布版本。
尽管仍在开发中,但为了想要使用官方客户端,我做了一个Elasticsearch CLI试试看。(虽然只有大约两个端点是运行的…)
https://github.com/bsoo/geso