gpt4 book ai didi

elasticsearch - 使用 go 在 elasticsearch 中进行大量更新

转载 作者:IT王子 更新时间:2023-10-29 02:09:33 33 4
gpt4 key购买 nike

我尝试用 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

}

elastic 中的 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(冲突)

最佳答案

我解决了这个问题:

out_name, err := client.UpdateByQuery().Index("test").Type("test").Query(query).Script(elastic.NewScriptInline("ctx._source.user_name = '" + requestName + "';ctx._source.user_nick = '" + requestNick + "';ctx._source.user_photo = '" + fullImageURL + "';ctx._source.user_thumb = '" + thumbnailURL + "'")).Do(ctx)
if nil != err {
log.Println(err)
}

关于elasticsearch - 使用 go 在 elasticsearch 中进行大量更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51367782/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com