search_source_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. "encoding/json"
  7. "testing"
  8. )
  9. func TestSearchSourceMatchAllQuery(t *testing.T) {
  10. matchAllQ := NewMatchAllQuery()
  11. builder := NewSearchSource().Query(matchAllQ)
  12. src, err := builder.Source()
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. data, err := json.Marshal(src)
  17. if err != nil {
  18. t.Fatalf("marshaling to JSON failed: %v", err)
  19. }
  20. got := string(data)
  21. expected := `{"query":{"match_all":{}}}`
  22. if got != expected {
  23. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  24. }
  25. }
  26. func TestSearchSourceNoStoredFields(t *testing.T) {
  27. matchAllQ := NewMatchAllQuery()
  28. builder := NewSearchSource().Query(matchAllQ).NoStoredFields()
  29. src, err := builder.Source()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. data, err := json.Marshal(src)
  34. if err != nil {
  35. t.Fatalf("marshaling to JSON failed: %v", err)
  36. }
  37. got := string(data)
  38. expected := `{"query":{"match_all":{}}}`
  39. if got != expected {
  40. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  41. }
  42. }
  43. func TestSearchSourceStoredFields(t *testing.T) {
  44. matchAllQ := NewMatchAllQuery()
  45. builder := NewSearchSource().Query(matchAllQ).StoredFields("message", "tags")
  46. src, err := builder.Source()
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. data, err := json.Marshal(src)
  51. if err != nil {
  52. t.Fatalf("marshaling to JSON failed: %v", err)
  53. }
  54. got := string(data)
  55. expected := `{"query":{"match_all":{}},"stored_fields":["message","tags"]}`
  56. if got != expected {
  57. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  58. }
  59. }
  60. func TestSearchSourceFetchSourceDisabled(t *testing.T) {
  61. matchAllQ := NewMatchAllQuery()
  62. builder := NewSearchSource().Query(matchAllQ).FetchSource(false)
  63. src, err := builder.Source()
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. data, err := json.Marshal(src)
  68. if err != nil {
  69. t.Fatalf("marshaling to JSON failed: %v", err)
  70. }
  71. got := string(data)
  72. expected := `{"_source":false,"query":{"match_all":{}}}`
  73. if got != expected {
  74. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  75. }
  76. }
  77. func TestSearchSourceFetchSourceByWildcards(t *testing.T) {
  78. matchAllQ := NewMatchAllQuery()
  79. fsc := NewFetchSourceContext(true).Include("obj1.*", "obj2.*").Exclude("*.description")
  80. builder := NewSearchSource().Query(matchAllQ).FetchSourceContext(fsc)
  81. src, err := builder.Source()
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. data, err := json.Marshal(src)
  86. if err != nil {
  87. t.Fatalf("marshaling to JSON failed: %v", err)
  88. }
  89. got := string(data)
  90. expected := `{"_source":{"excludes":["*.description"],"includes":["obj1.*","obj2.*"]},"query":{"match_all":{}}}`
  91. if got != expected {
  92. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  93. }
  94. }
  95. func TestSearchSourceDocvalueFields(t *testing.T) {
  96. matchAllQ := NewMatchAllQuery()
  97. builder := NewSearchSource().Query(matchAllQ).DocvalueFields("test1", "test2")
  98. src, err := builder.Source()
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. data, err := json.Marshal(src)
  103. if err != nil {
  104. t.Fatalf("marshaling to JSON failed: %v", err)
  105. }
  106. got := string(data)
  107. expected := `{"docvalue_fields":["test1","test2"],"query":{"match_all":{}}}`
  108. if got != expected {
  109. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  110. }
  111. }
  112. func TestSearchSourceScriptFields(t *testing.T) {
  113. matchAllQ := NewMatchAllQuery()
  114. sf1 := NewScriptField("test1", NewScript("doc['my_field_name'].value * 2"))
  115. sf2 := NewScriptField("test2", NewScript("doc['my_field_name'].value * factor").Param("factor", 3.1415927))
  116. builder := NewSearchSource().Query(matchAllQ).ScriptFields(sf1, sf2)
  117. src, err := builder.Source()
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. data, err := json.Marshal(src)
  122. if err != nil {
  123. t.Fatalf("marshaling to JSON failed: %v", err)
  124. }
  125. got := string(data)
  126. expected := `{"query":{"match_all":{}},"script_fields":{"test1":{"script":{"inline":"doc['my_field_name'].value * 2"}},"test2":{"script":{"inline":"doc['my_field_name'].value * factor","params":{"factor":3.1415927}}}}}`
  127. if got != expected {
  128. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  129. }
  130. }
  131. func TestSearchSourcePostFilter(t *testing.T) {
  132. matchAllQ := NewMatchAllQuery()
  133. pf := NewTermQuery("tag", "important")
  134. builder := NewSearchSource().Query(matchAllQ).PostFilter(pf)
  135. src, err := builder.Source()
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. data, err := json.Marshal(src)
  140. if err != nil {
  141. t.Fatalf("marshaling to JSON failed: %v", err)
  142. }
  143. got := string(data)
  144. expected := `{"post_filter":{"term":{"tag":"important"}},"query":{"match_all":{}}}`
  145. if got != expected {
  146. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  147. }
  148. }
  149. func TestSearchSourceHighlight(t *testing.T) {
  150. matchAllQ := NewMatchAllQuery()
  151. hl := NewHighlight().Field("content")
  152. builder := NewSearchSource().Query(matchAllQ).Highlight(hl)
  153. src, err := builder.Source()
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. data, err := json.Marshal(src)
  158. if err != nil {
  159. t.Fatalf("marshaling to JSON failed: %v", err)
  160. }
  161. got := string(data)
  162. expected := `{"highlight":{"fields":{"content":{}}},"query":{"match_all":{}}}`
  163. if got != expected {
  164. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  165. }
  166. }
  167. func TestSearchSourceRescoring(t *testing.T) {
  168. matchAllQ := NewMatchAllQuery()
  169. rescorerQuery := NewMatchPhraseQuery("field1", "the quick brown fox").Slop(2)
  170. rescorer := NewQueryRescorer(rescorerQuery)
  171. rescorer = rescorer.QueryWeight(0.7)
  172. rescorer = rescorer.RescoreQueryWeight(1.2)
  173. rescore := NewRescore().WindowSize(50).Rescorer(rescorer)
  174. builder := NewSearchSource().Query(matchAllQ).Rescorer(rescore)
  175. src, err := builder.Source()
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. data, err := json.Marshal(src)
  180. if err != nil {
  181. t.Fatalf("marshaling to JSON failed: %v", err)
  182. }
  183. got := string(data)
  184. expected := `{"query":{"match_all":{}},"rescore":{"query":{"query_weight":0.7,"rescore_query":{"match_phrase":{"field1":{"query":"the quick brown fox","slop":2}}},"rescore_query_weight":1.2},"window_size":50}}`
  185. if got != expected {
  186. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  187. }
  188. }
  189. func TestSearchSourceIndexBoost(t *testing.T) {
  190. matchAllQ := NewMatchAllQuery()
  191. builder := NewSearchSource().Query(matchAllQ).IndexBoost("index1", 1.4).IndexBoost("index2", 1.3)
  192. src, err := builder.Source()
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. data, err := json.Marshal(src)
  197. if err != nil {
  198. t.Fatalf("marshaling to JSON failed: %v", err)
  199. }
  200. got := string(data)
  201. expected := `{"indices_boost":{"index1":1.4,"index2":1.3},"query":{"match_all":{}}}`
  202. if got != expected {
  203. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  204. }
  205. }
  206. func TestSearchSourceMixDifferentSorters(t *testing.T) {
  207. matchAllQ := NewMatchAllQuery()
  208. builder := NewSearchSource().Query(matchAllQ).
  209. Sort("a", false).
  210. SortWithInfo(SortInfo{Field: "b", Ascending: true}).
  211. SortBy(NewScriptSort(NewScript("doc['field_name'].value * factor").Param("factor", 1.1), "number"))
  212. src, err := builder.Source()
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. data, err := json.Marshal(src)
  217. if err != nil {
  218. t.Fatalf("marshaling to JSON failed: %v", err)
  219. }
  220. got := string(data)
  221. expected := `{"query":{"match_all":{}},"sort":[{"a":{"order":"desc"}},{"b":{"order":"asc"}},{"_script":{"order":"asc","script":{"inline":"doc['field_name'].value * factor","params":{"factor":1.1}},"type":"number"}}]}`
  222. if got != expected {
  223. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  224. }
  225. }
  226. func TestSearchSourceInnerHits(t *testing.T) {
  227. matchAllQ := NewMatchAllQuery()
  228. builder := NewSearchSource().Query(matchAllQ).
  229. InnerHit("comments", NewInnerHit().Type("comment").Query(NewMatchQuery("user", "olivere"))).
  230. InnerHit("views", NewInnerHit().Path("view"))
  231. src, err := builder.Source()
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. data, err := json.Marshal(src)
  236. if err != nil {
  237. t.Fatalf("marshaling to JSON failed: %v", err)
  238. }
  239. got := string(data)
  240. expected := `{"inner_hits":{"comments":{"type":{"comment":{"query":{"match":{"user":{"query":"olivere"}}}}}},"views":{"path":{"view":{}}}},"query":{"match_all":{}}}`
  241. if got != expected {
  242. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  243. }
  244. }
  245. func TestSearchSourceSearchAfter(t *testing.T) {
  246. matchAllQ := NewMatchAllQuery()
  247. builder := NewSearchSource().Query(matchAllQ).SearchAfter(1463538857, "tweet#654323")
  248. src, err := builder.Source()
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. data, err := json.Marshal(src)
  253. if err != nil {
  254. t.Fatalf("marshaling to JSON failed: %v", err)
  255. }
  256. got := string(data)
  257. expected := `{"query":{"match_all":{}},"search_after":[1463538857,"tweet#654323"]}`
  258. if got != expected {
  259. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  260. }
  261. }
  262. func TestSearchSourceProfiledQuery(t *testing.T) {
  263. matchAllQ := NewMatchAllQuery()
  264. builder := NewSearchSource().Query(matchAllQ).Profile(true)
  265. src, err := builder.Source()
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. data, err := json.Marshal(src)
  270. if err != nil {
  271. t.Fatalf("marshaling to JSON failed: %v", err)
  272. }
  273. got := string(data)
  274. expected := `{"profile":true,"query":{"match_all":{}}}`
  275. if got != expected {
  276. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  277. }
  278. }