update_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. "net/url"
  9. "testing"
  10. )
  11. func TestUpdateViaScript(t *testing.T) {
  12. client := setupTestClient(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  13. update := client.Update().
  14. Index("test").Type("type1").Id("1").
  15. Script(NewScript("ctx._source.tags += tag").Params(map[string]interface{}{"tag": "blue"}).Lang("groovy"))
  16. path, params, err := update.url()
  17. if err != nil {
  18. t.Fatalf("expected to return URL, got: %v", err)
  19. }
  20. expectedPath := `/test/type1/1/_update`
  21. if expectedPath != path {
  22. t.Errorf("expected URL path\n%s\ngot:\n%s", expectedPath, path)
  23. }
  24. expectedParams := url.Values{}
  25. if expectedParams.Encode() != params.Encode() {
  26. t.Errorf("expected URL parameters\n%s\ngot:\n%s", expectedParams.Encode(), params.Encode())
  27. }
  28. body, err := update.body()
  29. if err != nil {
  30. t.Fatalf("expected to return body, got: %v", err)
  31. }
  32. data, err := json.Marshal(body)
  33. if err != nil {
  34. t.Fatalf("expected to marshal body as JSON, got: %v", err)
  35. }
  36. got := string(data)
  37. expected := `{"script":{"inline":"ctx._source.tags += tag","lang":"groovy","params":{"tag":"blue"}}}`
  38. if got != expected {
  39. t.Errorf("expected\n%s\ngot:\n%s", expected, got)
  40. }
  41. }
  42. func TestUpdateViaScriptId(t *testing.T) {
  43. client := setupTestClient(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  44. scriptParams := map[string]interface{}{
  45. "pageViewEvent": map[string]interface{}{
  46. "url": "foo.com/bar",
  47. "response": 404,
  48. "time": "2014-01-01 12:32",
  49. },
  50. }
  51. script := NewScriptId("my_web_session_summariser").Params(scriptParams)
  52. update := client.Update().
  53. Index("sessions").Type("session").Id("dh3sgudg8gsrgl").
  54. Script(script).
  55. ScriptedUpsert(true).
  56. Upsert(map[string]interface{}{})
  57. path, params, err := update.url()
  58. if err != nil {
  59. t.Fatalf("expected to return URL, got: %v", err)
  60. }
  61. expectedPath := `/sessions/session/dh3sgudg8gsrgl/_update`
  62. if expectedPath != path {
  63. t.Errorf("expected URL path\n%s\ngot:\n%s", expectedPath, path)
  64. }
  65. expectedParams := url.Values{}
  66. if expectedParams.Encode() != params.Encode() {
  67. t.Errorf("expected URL parameters\n%s\ngot:\n%s", expectedParams.Encode(), params.Encode())
  68. }
  69. body, err := update.body()
  70. if err != nil {
  71. t.Fatalf("expected to return body, got: %v", err)
  72. }
  73. data, err := json.Marshal(body)
  74. if err != nil {
  75. t.Fatalf("expected to marshal body as JSON, got: %v", err)
  76. }
  77. got := string(data)
  78. expected := `{"script":{"id":"my_web_session_summariser","params":{"pageViewEvent":{"response":404,"time":"2014-01-01 12:32","url":"foo.com/bar"}}},"scripted_upsert":true,"upsert":{}}`
  79. if got != expected {
  80. t.Errorf("expected\n%s\ngot:\n%s", expected, got)
  81. }
  82. }
  83. func TestUpdateViaScriptFile(t *testing.T) {
  84. client := setupTestClient(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  85. scriptParams := map[string]interface{}{
  86. "pageViewEvent": map[string]interface{}{
  87. "url": "foo.com/bar",
  88. "response": 404,
  89. "time": "2014-01-01 12:32",
  90. },
  91. }
  92. script := NewScriptFile("update_script").Params(scriptParams)
  93. update := client.Update().
  94. Index("sessions").Type("session").Id("dh3sgudg8gsrgl").
  95. Script(script).
  96. ScriptedUpsert(true).
  97. Upsert(map[string]interface{}{})
  98. path, params, err := update.url()
  99. if err != nil {
  100. t.Fatalf("expected to return URL, got: %v", err)
  101. }
  102. expectedPath := `/sessions/session/dh3sgudg8gsrgl/_update`
  103. if expectedPath != path {
  104. t.Errorf("expected URL path\n%s\ngot:\n%s", expectedPath, path)
  105. }
  106. expectedParams := url.Values{}
  107. if expectedParams.Encode() != params.Encode() {
  108. t.Errorf("expected URL parameters\n%s\ngot:\n%s", expectedParams.Encode(), params.Encode())
  109. }
  110. body, err := update.body()
  111. if err != nil {
  112. t.Fatalf("expected to return body, got: %v", err)
  113. }
  114. data, err := json.Marshal(body)
  115. if err != nil {
  116. t.Fatalf("expected to marshal body as JSON, got: %v", err)
  117. }
  118. got := string(data)
  119. expected := `{"script":{"file":"update_script","params":{"pageViewEvent":{"response":404,"time":"2014-01-01 12:32","url":"foo.com/bar"}}},"scripted_upsert":true,"upsert":{}}`
  120. if got != expected {
  121. t.Errorf("expected\n%s\ngot:\n%s", expected, got)
  122. }
  123. }
  124. func TestUpdateViaScriptAndUpsert(t *testing.T) {
  125. client := setupTestClient(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  126. update := client.Update().
  127. Index("test").Type("type1").Id("1").
  128. Script(NewScript("ctx._source.counter += count").Params(map[string]interface{}{"count": 4})).
  129. Upsert(map[string]interface{}{"counter": 1})
  130. path, params, err := update.url()
  131. if err != nil {
  132. t.Fatalf("expected to return URL, got: %v", err)
  133. }
  134. expectedPath := `/test/type1/1/_update`
  135. if expectedPath != path {
  136. t.Errorf("expected URL path\n%s\ngot:\n%s", expectedPath, path)
  137. }
  138. expectedParams := url.Values{}
  139. if expectedParams.Encode() != params.Encode() {
  140. t.Errorf("expected URL parameters\n%s\ngot:\n%s", expectedParams.Encode(), params.Encode())
  141. }
  142. body, err := update.body()
  143. if err != nil {
  144. t.Fatalf("expected to return body, got: %v", err)
  145. }
  146. data, err := json.Marshal(body)
  147. if err != nil {
  148. t.Fatalf("expected to marshal body as JSON, got: %v", err)
  149. }
  150. got := string(data)
  151. expected := `{"script":{"inline":"ctx._source.counter += count","params":{"count":4}},"upsert":{"counter":1}}`
  152. if got != expected {
  153. t.Errorf("expected\n%s\ngot:\n%s", expected, got)
  154. }
  155. }
  156. func TestUpdateViaDoc(t *testing.T) {
  157. client := setupTestClient(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  158. update := client.Update().
  159. Index("test").Type("type1").Id("1").
  160. Doc(map[string]interface{}{"name": "new_name"}).
  161. DetectNoop(true)
  162. path, params, err := update.url()
  163. if err != nil {
  164. t.Fatalf("expected to return URL, got: %v", err)
  165. }
  166. expectedPath := `/test/type1/1/_update`
  167. if expectedPath != path {
  168. t.Errorf("expected URL path\n%s\ngot:\n%s", expectedPath, path)
  169. }
  170. expectedParams := url.Values{}
  171. if expectedParams.Encode() != params.Encode() {
  172. t.Errorf("expected URL parameters\n%s\ngot:\n%s", expectedParams.Encode(), params.Encode())
  173. }
  174. body, err := update.body()
  175. if err != nil {
  176. t.Fatalf("expected to return body, got: %v", err)
  177. }
  178. data, err := json.Marshal(body)
  179. if err != nil {
  180. t.Fatalf("expected to marshal body as JSON, got: %v", err)
  181. }
  182. got := string(data)
  183. expected := `{"detect_noop":true,"doc":{"name":"new_name"}}`
  184. if got != expected {
  185. t.Errorf("expected\n%s\ngot:\n%s", expected, got)
  186. }
  187. }
  188. func TestUpdateViaDocAndUpsert(t *testing.T) {
  189. client := setupTestClient(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  190. update := client.Update().
  191. Index("test").Type("type1").Id("1").
  192. Doc(map[string]interface{}{"name": "new_name"}).
  193. DocAsUpsert(true).
  194. Timeout("1s").
  195. Refresh("true")
  196. path, params, err := update.url()
  197. if err != nil {
  198. t.Fatalf("expected to return URL, got: %v", err)
  199. }
  200. expectedPath := `/test/type1/1/_update`
  201. if expectedPath != path {
  202. t.Errorf("expected URL path\n%s\ngot:\n%s", expectedPath, path)
  203. }
  204. expectedParams := url.Values{"refresh": []string{"true"}, "timeout": []string{"1s"}}
  205. if expectedParams.Encode() != params.Encode() {
  206. t.Errorf("expected URL parameters\n%s\ngot:\n%s", expectedParams.Encode(), params.Encode())
  207. }
  208. body, err := update.body()
  209. if err != nil {
  210. t.Fatalf("expected to return body, got: %v", err)
  211. }
  212. data, err := json.Marshal(body)
  213. if err != nil {
  214. t.Fatalf("expected to marshal body as JSON, got: %v", err)
  215. }
  216. got := string(data)
  217. expected := `{"doc":{"name":"new_name"},"doc_as_upsert":true}`
  218. if got != expected {
  219. t.Errorf("expected\n%s\ngot:\n%s", expected, got)
  220. }
  221. }
  222. func TestUpdateViaDocAndUpsertAndFetchSource(t *testing.T) {
  223. client := setupTestClient(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  224. update := client.Update().
  225. Index("test").Type("type1").Id("1").
  226. Doc(map[string]interface{}{"name": "new_name"}).
  227. DocAsUpsert(true).
  228. Timeout("1s").
  229. Refresh("true").
  230. FetchSource(true)
  231. path, params, err := update.url()
  232. if err != nil {
  233. t.Fatalf("expected to return URL, got: %v", err)
  234. }
  235. expectedPath := `/test/type1/1/_update`
  236. if expectedPath != path {
  237. t.Errorf("expected URL path\n%s\ngot:\n%s", expectedPath, path)
  238. }
  239. expectedParams := url.Values{
  240. "refresh": []string{"true"},
  241. "timeout": []string{"1s"},
  242. }
  243. if expectedParams.Encode() != params.Encode() {
  244. t.Errorf("expected URL parameters\n%s\ngot:\n%s", expectedParams.Encode(), params.Encode())
  245. }
  246. body, err := update.body()
  247. if err != nil {
  248. t.Fatalf("expected to return body, got: %v", err)
  249. }
  250. data, err := json.Marshal(body)
  251. if err != nil {
  252. t.Fatalf("expected to marshal body as JSON, got: %v", err)
  253. }
  254. got := string(data)
  255. expected := `{"_source":true,"doc":{"name":"new_name"},"doc_as_upsert":true}`
  256. if got != expected {
  257. t.Errorf("expected\n%s\ngot:\n%s", expected, got)
  258. }
  259. }
  260. func TestUpdateAndFetchSource(t *testing.T) {
  261. client := setupTestClientAndCreateIndexAndAddDocs(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  262. res, err := client.Update().
  263. Index(testIndexName).Type("tweet").Id("1").
  264. Doc(map[string]interface{}{"user": "sandrae"}).
  265. DetectNoop(true).
  266. FetchSource(true).
  267. Do(context.Background())
  268. if err != nil {
  269. t.Fatal(err)
  270. }
  271. if res == nil {
  272. t.Fatal("expected response != nil")
  273. }
  274. if res.GetResult == nil {
  275. t.Fatal("expected GetResult != nil")
  276. }
  277. data, err := json.Marshal(res.GetResult.Source)
  278. if err != nil {
  279. t.Fatalf("expected to marshal body as JSON, got: %v", err)
  280. }
  281. got := string(data)
  282. expected := `{"user":"sandrae","message":"Welcome to Golang and Elasticsearch.","retweets":0,"created":"0001-01-01T00:00:00Z"}`
  283. if got != expected {
  284. t.Errorf("expected\n%s\ngot:\n%s", expected, got)
  285. }
  286. }