惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
O
OpenAI News
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
Docker
F
Full Disclosure
AI
AI
罗磊的独立博客
博客园 - 【当耐特】
U
Unit 42
S
SegmentFault 最新的问题
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Palo Alto Networks Blog
博客园_首页
H
Help Net Security
量子位
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog RSS Feed
Webroot Blog
Webroot Blog
TaoSecurity Blog
TaoSecurity Blog
S
Secure Thoughts
爱范儿
爱范儿
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
CERT Recently Published Vulnerability Notes
Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Securelist

博客园 - 柒零壹

Ubuntu 24.04 安装最新版podman@5.6.1 [转贴]在前端如何玩转 Word 文档 一个命令行参数解决open-webui镜像启动失败的问题(huggingface网站访问失败问题) Go Template 常用疑难知识点 从数据库中随机选取数据(基于golang,xorm) go-ElasticSearch TypedClient学习笔记 golang用pgx查询数据时如何将查询结果方便的放入Map中 【转】网络常用颜文字(文字表情) GoCV下实现多图片单窗口内同时显示 解决GoCV/OpenCV不支持中文的问题 golang中xorm自动维护表结构自动导入数据的实现 [golang] gin的中间中调用Abort方法导致的带附件的表单提交时,浏览器报net::ERR_CONNECTION_RESET错误的原因及解决方法 [golang]filepath.Glob的缺陷,不支持多级目录 浏览器中javascript简易实现json数据保存到客户端 最简单搭建前端轻量级项目开发服务 [转]Three.js做一个酷炫的城市展示可视化大屏 [转]css实现不同样式的tooltip对话框小三角 [原]升级项目到Rails7.0.3,使用自己手动方案编译打包css及js资源 puma web server如何监听所有IP地址
go语言如何使用elastic官方客户端go-elasticsearch/v8实现数据批量更新
柒零壹 · 2023-07-31 · via 博客园 - 柒零壹

go语言如何使用elastic官方客户端go-elasticsearch/v8实现数据批量更新

go语言的官方客户端

Elasticsearch 的官方 Go 客户端是由 Elastic 开发、维护和支持的客户端系列的最新成员之一。 初始版本于 2019 年初发布,并在过去几年中逐渐成熟,获得了重试请求、发现集群节点和各种辅助组件等功能。

以上来自”Elastic 中国社区官方博客

现在最新版已经是v8了,就在不久前,我刚刚更新到新鲜出炉的v8@8.9.0。由于v8版本变动较大,网上不多的基于golang的例子都几乎不能用了,最好还是参考上边所提到的Elastic 中国社区官方博客和官网上的例子。

Bulk功能必须使用es.Client

一开始我选择了使用es.TypedClient,虽然使用起来麻烦点儿,但毕竟是强类型的,使用还算是顺利的,直到我开始打算使用Bulk批量更新。到目前为止,我在必应上是搜索不到基于v8的Bulk使用例子,没办法只好在github官网的源代码库里找到_example目录下单范例,后来还找到这篇文章

引用博文中的一段话:

 One of the most common use cases for any Elasticsearch client is indexing documents into Elasticsearch as quickly and efficiently as possible. The most straightforward option, using the plain Elasticsearch Bulk API, comes with a lot of drawbacks: you have to manually prepare the meta and data pairs of the payload, divide the payload into batches, deserialize the response, inspect the results for errors, display a report, and so on. The default example in the repository demonstrates quite eloquently how involved it all is.
 
 For that reason, the client provides a helper component, esutil.BulkIndexer, similar to bulk helpers in other clients:

基于同样的原因,我也选择了使用esutil.BulkIndexer,因为看上去的确方便了许多。但是当我开始着手编码,立马蒙圈了,TypedClient根本没有Bulk这个方法!......过程不提,最后,我还是没辙了,决定同时在连接一个普通的Client类型。

第一次暴击

照猫画虎很简单,很快实现了我的bulk版本方法:

 func (me *YhkbElasticReceiver) bulkUpsertKnowledge(datas  []*ElKnowledge)(){  
  var countSuccessful uint64
 

马上试运行了一下,立马遭受一万点暴击,出错了:

 [YhkbElReceiver.BulkUpsertKnowledge]处理数据[0]行,其中失败[100], 耗时 19ms (速度:0 docs/秒)

100%的失败了,而且BulkIndexerItem.OnFailure方法毫无动静,真是莫名其妙。还好看到了BulkIndexerConfig有OnError回调,赶紧注册个回调函数,这回终于不再沉默,打印出来了如下的错误:

 error":{"root_cause":[
 {"type":"x_content_parse_exception","reason":"[1:2] [UpdateRequest] unknown field [id]"}],
     "type":"x_content_parse_exception","reason":"[1:2] [UpdateRequest] unknown field [id]"},"status":400}

经过一番网上搜索,说是文档格式不对!后参考了一下直接用HTTP POST实现的代码,灵光乍现,包装了一下我的实体类序列化的JSON字符串,也就是重新拼接了一下上面代码中payload这一行:

 payload:=fmt.Sprintf(`{"doc":%s,"doc_as_upsert":true}`,data.ToJson())

再次运行程序,成功了! w(゚Д゚)w (ノへ ̄、)