Skip to content
jfcantu edited this page May 31, 2020 · 5 revisions

The Update service allows you to update documents.

v3 and above:

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)

Prior to v3:

// 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)
Clone this wiki locally