【发布时间】:2019-03-05 12:05:14
【问题描述】:
我尝试用 golang 更新我的 elasticsearch 数据库。我有两个功能:
func UpdateAllByUserID(client *elastic.Client, id string, requestName string, requestNick string) error {
ctx := context.Background()
query := elastic.NewMatchQuery("user_id", id)
out_name, err := client.UpdateByQuery().Index("test").Type("test").Query(query).Script(elastic.NewScriptInline("ctx._source.user_name = '" + requestName + "'")).Do(ctx)
if nil != err {
log.Println(err)
}
fmt.Println("update all name: ", out_name.Updated)
return nil
}
func UpdateAllNicksByUserIdInFeed(client *elastic.Client, id string, requestNick string) error {
ctx := context.Background()
query := elastic.NewMatchQuery("user_id", id)
out_nick, err := client.UpdateByQuery().Index("test").Type("test").Query(query).Script(elastic.NewScriptInline("ctx._source.user_nick = '" + requestNick + "'")).Do(ctx)
if nil != err {
log.Println(err)
}
fmt.Println("update all nick: ", out_nick.Updated)
return nil
}
弹性中的 POST:
POST {index}/{type}/_update_by_query
{
"script": {
"inline": "ctx._source.user_name = 'test'",
"inline": "ctx._source.user_nick = 'test test'"
},
"query": {
"match": {
"user_id": "mtJZngDOy6Qj22Qv9MEf1MhSLVb2"
}
}
}
我正在使用库 github.com/olivere/elastic。 elasticsearch的版本是5.6 这个功能每个单独它运作良好,但我有两个问题:
如何在同一个函数中更新? 为什么同时使用这两个函数我有这个错误:
弹性:错误 409(冲突)
【问题讨论】:
标签: elasticsearch go