update_integration_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "testing"
  9. )
  10. func TestUpdateWithScript(t *testing.T) {
  11. client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
  12. // Get original
  13. getRes, err := client.Get().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO())
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. var original tweet
  18. if err := json.Unmarshal(*getRes.Source, &original); err != nil {
  19. t.Fatal(err)
  20. }
  21. // Update with script
  22. updRes, err := client.Update().Index(testIndexName).Type("tweet").Id("1").
  23. Script(
  24. NewScript(`ctx._source.message = "Updated message text."`).Lang("painless"),
  25. ).
  26. Do(context.TODO())
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if updRes == nil {
  31. t.Fatal("response is nil")
  32. }
  33. if want, have := "updated", updRes.Result; want != have {
  34. t.Fatalf("want Result = %q, have %v", want, have)
  35. }
  36. // Get new version
  37. getRes, err = client.Get().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO())
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. var updated tweet
  42. if err := json.Unmarshal(*getRes.Source, &updated); err != nil {
  43. t.Fatal(err)
  44. }
  45. if want, have := original.User, updated.User; want != have {
  46. t.Fatalf("want User = %q, have %v", want, have)
  47. }
  48. if want, have := "Updated message text.", updated.Message; want != have {
  49. t.Fatalf("want Message = %q, have %v", want, have)
  50. }
  51. }