update_test.go 9.3 KB

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