UpdateByQuery Golang Gin API|Elasticsearch Guide
環境配置
開發環境:MacOS Big Sur 11.2.3
部署方式:Docker Engine 20.10.5
Elasticsearch 版本:7.11
Goalng 版本:1.15
Gin Framework:v1.7.2
Golang elasticsearch client:v8
安裝 ES Official Library
在 go.mod 中加入以下字段:
require github.com/elastic/go-elasticsearch/v8 master
建立 DSL Model
這邊先建立 model,或是用 map 也可以,但因為 elasticsearch 的 DSL 通常較為複雜,我這邊選擇用 model(左圖),日後也較好維護。建立後,根據需求組合最終的 DSL(右圖),等下可以帶入 Elasticsearch client。
package models
type UpdateByQueryDSL struct {
Query DSLQuery `json:"query"`
Script string `json:"script"`
}
type DSLQuery struct {
Bool DSLBool `json:"bool"`
}
type DSLBool struct {
Should []DSLShould `json:"should"`
}
type DSLShould struct {
MatchPhrase DSLMatchPhrase `json:"match_phrase"`
}
type DSLMatchPhrase struct {
EventID string `json:"eventid.keyword"`
}
{
"query": {
"bool": {
"should": [ {
"match_phrase": {
"eventid.keyword": "12345678"
}
},
{
"match_phrase": {
"eventid.keyword": "87654321"
}
}
]
}
},
"script": "ctx._source.NOC_Note = '很菜'"
}