版权声明:本文为博主原创文章,转载请注明出处:https://twocups.cn/index.php/2021/10/29/46/

结论先行

直接用 Golang 模拟 HTTP 请求访问 Elasticsearch 最方便了!(官方的 SDK 就是类似的做法)

前言

因工作需要,要定期从 Elasticsearch(以下简称es)中取数据。我选的编程语言是 Golang,下面讲一下我自己在用 Golang 查询 es 数据的时候遇到的坑。

注意事项

1. Golang操作Elasticsearch引用的SDK

主流的SDK有两种

  • github.com/olivere/elastic (第三方)
  • github.com/elastic/go-elasticsearch (官方)

一般来说,官方的 SDK 最好的,因为支持性是最强的,所以我一般都用官方给的 SDK。但第三方SDK还是很火,所以这次我来试试看。(从体验结果上来说,还是官方的好用)

2. SDK版本和Elasticsearch版本要对齐

如果不对齐,使用 SDK 向 es 请求的时候就会报错。一般 SDK 和 es 的版本号是相同的,例如5.x、6.x、7.x。

如果不知道目标 es 的版本是多少,可以直接在 es 里面查一下。

curl http://es_ip:port

{
 "name" : "node-1",
 "cluster_name" : "elasticsearch",
 "cluster_uuid" : "",
 "version" : {
   "number" : "7.13.1",
   "build_flavor" : "default",
   "build_type" : "tar",
   "build_hash" : "",
   "build_date" : "2021-05-28T17:40:59.346932922Z",
   "build_snapshot" : false,
   "lucene_version" : "8.8.2",
   "minimum_wire_compatibility_version" : "6.8.0",
   "minimum_index_compatibility_version" : "6.0.0-beta1"
},
 "tagline" : "You Know, for Search"
}

我这个测试环境里面的 es 版本就是7.13.1,然后下载对应的 SDK 就行了。

3. 创建Elasticsearch客户端失败

有人在创建 es 客户端的时候会遇到这个报错。

context deadline exceeded

这是因为 github.com/olivere/elastic 这个包会自动转换连接地址,所以需要设置 elastic.SetSniff 为 false。

// 创建client连接ES
client, err := elastic.NewClient(
   elastic.SetURL(esUrl),
 elastic.SetBasicAuth(esId, esKey),
 // 这里设置成不需要地址自动转换,否则会报错context deadline exceeded
 elastic.SetSniff(false),
)

4. Elasticsearch查询结果的数据格式转换

使用 Golang 给 es 做查询是这样的。

response, err := client.Get().
Index(index).Type(type).
Id(id).
Do(context.Background())

但是这个 response 的数据格式是 *GetService,并且查询到的数据是在 response.Source 中。而这个 response.Source 的数据结构是 *json.RawMessage,这是一种原始的 json 结构,不能直接读。但毕竟算是 json,所以可以用一个数据结构把这个 json 里面的需要的数据映射出来。

type Data struct {
Name string `json:"name"`
Age string `json:"age"`
}

func GetData(client *elastic.Client, index string) {
 res, err := client.Get().Index(index).Type("_doc").Id("userTotal-2021-10-14").Do(context.Background())
if err != nil {
fmt.Println("查询数据错误")
return
}

if res.Found {
var msg ProxyData
// 这里别忘了*,因为res.Source是地址
    if err := json.Unmarshal(*res.Source, &msg); err != nil {
fmt.Println("转换成json失败")
        return
}
fmt.Println(msg)
}
}

直接用HTTP请求访问Elasticsearch

就我目前三种 Golang 操作 es 的方式(官方 SDK、第三方 SDK、直接HTTP请求)使用下来,最方便的其实还是直接用 HTTP 请求直接访问。因为使用 HTTP 直接访问可以直接用 es 的查询语法,而不需要去学习 SDK 的逻辑。第二方便的就是官方的 SDK,现在最新的官网上也可以直接用 es 的语法,而不用研究 SDK 的具体使用方法。最后才是第三方包。

我有之前自己封装好的 HTTP 请求的代码,而且这种对 es 的操作是绝大部分情况是不需要 tcp 去保持连接的。所以对于我来说,Golang 直接通过 HTTP 请求 es 是最好用的。

林皓伟

《【Elastic Stack系列】第五章:工作中实践(一):Golang操作Elasticsearch注意事项》有 10 条评论
  1. I have noticed that online degree is getting common because obtaining your college degree online has turned into a popular method for many people. Many people have not had a chance to attend an established college or university however seek the raised earning possibilities and a better job that a Bachelors Degree gives. Still other individuals might have a degree in one field but would want to pursue one thing they now have an interest in. Elvis Pitner

  2. I want to express my respect for your generosity for those who should have help on this particular matter. Your real commitment to passing the solution throughout has been definitely interesting and have consistently allowed employees like me to arrive at their ambitions. This warm and friendly facts signifies a great deal to me and much more to my office colleagues. Regards; from each one of us. Hosea Provow

  3. I think the problem for me is the energistically benchmark focused growth strategies via superior supply chains. Compellingly reintermediate mission-critical potentialities whereas cross functional scenarios. Phosfluorescently re-engineer distributed processes without standardized supply chains. Quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Stefan Linman

  4. I do love the way you have framed this particular difficulty and it does present me personally some fodder for consideration. However, through everything that I have seen, I only wish when the reviews pack on that people continue to be on point and in no way start upon a tirade involving the news of the day. All the same, thank you for this fantastic piece and whilst I can not really go along with the idea in totality, I respect the viewpoint. Thaddeus Aney

  5. Dazbet ialah situs judi online Indonesia terpercaya resmi saat ini yang ada untuk memenuhi kebutuhan member judi online dalam bermain dan mencari penghasilan tambahan dengan bermain bersama kami. Dengan perkembangan teknologi yang sudah pesat jaman ini kalian bisa memainkan beragam permainan judi online hanya dengan membutuhkan jaringan internet dan juga gadget kalian, baik itu android, ios, laptop, dan juga pc kalian dimana saja dan kapan saja dengan cukup membuka situs kami dan bertaruh di dalamnya. Junior Kortkamp

  6. Needed to write you this very small word to help thank you very much yet again with the superb tricks you have shown on this website. This is so unbelievably open-handed with you to provide without restraint what exactly numerous people would have supplied for an electronic book to make some profit on their own, principally since you might well have tried it in the event you wanted. Those thoughts likewise worked to provide a great way to understand that the rest have the same zeal the same as my very own to understand way more with regards to this problem. I think there are thousands of more pleasurable times up front for those who take a look at your website. Hiram Picon

  7. I have been surfing on-line more than three hours these days, but I never found any attention-grabbing article like yours. It is pretty price enough for me. Personally, if all website owners and bloggers made just right content as you probably did, the internet shall be much more useful than ever before. Scottie Thanpaeng

  8. Read more about wholesome k9 deals with. In any structure pan, wake collectively all the wheat or grain flour, powder dairy, wheat or grain inspiring seed, and also peanut butter. Include glaciers the water 1 tablespoon at a stretch till cash might be humid adequate to help you hold fast collectively. Owen Nigro

发表回复

您的电子邮箱地址不会被公开。