-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Update
Lingchao Xin edited this page Mar 1, 2018
·
5 revisions
The Update
service allows you to update documents.
// Update a tweet by the update API of Elasticsearch.
// We just increment the number of retweets.
ctx := context.Background()
update, err := client.Update().Index("twitter").Type("tweet").Id("1").
Script("ctx._source.retweets += num").
ScriptParams(map[string]interface{}{"num": 1}).
Upsert(map[string]interface{}{"retweets": 0}).
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("New version of tweet %q is now %d", update.Id, update.Version)
v5
version:
ctx := context.Background()
update, err := client.Update().Index("twitter").Type("tweet").Id("1").
Script(elastic.NewScript("ctx._source.retweets += params.num").Param("num", 1)).
Upsert(map[string]interface{}{"retweets": 0}).
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("New version of tweet %q is now %d", update.Id, update.Version)