Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use upsert ? can you give a example? #76

Closed
tx991020 opened this issue Aug 3, 2019 · 1 comment
Closed

how to use upsert ? can you give a example? #76

tx991020 opened this issue Aug 3, 2019 · 1 comment

Comments

@tx991020
Copy link

tx991020 commented Aug 3, 2019

like this in java

IndexRequest indexRequest = new IndexRequest("index", "type", "1")
.source(jsonBuilder()
.startObject()
.field("name", "Joe Smith")
.field("gender", "male")
.endObject());
UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
.doc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject())
.upsert(indexRequest);
client.update(updateRequest).get();

@karmi
Copy link
Contributor

karmi commented Aug 7, 2019

Hi, the client doesn't have any helper methods for programatically building the body — it accepts an io.Reader. Hence, following the documentation for upsert, you have to provide the body somehow like this:

//+build ignore

package main

import (
	"log"
	"strings"

	"github.com/elastic/go-elasticsearch/v8"
)

func main() {
	log.SetFlags(0)

	es, err := elasticsearch.NewDefaultClient()
	if err != nil {
		log.Fatalf("Error creating the client: %s", err)
	}

	res, err := es.Update(
		"test",
		"1",
		strings.NewReader(`{
      "script" : {
        "source": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
      },
      "upsert" : {
        "counter" : 1
      }
    }`),
	)

	log.Println(res.Status(), err)

	log.Println(es.Get("test", "1"))
}

@tx991020 tx991020 closed this as completed Aug 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants