search_source_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "testing"
  8. )
  9. func TestSearchSourceMatchAllQuery(t *testing.T) {
  10. matchAllQ := NewMatchAllQuery()
  11. builder := NewSearchSource().Query(matchAllQ)
  12. data, err := json.Marshal(builder.Source())
  13. if err != nil {
  14. t.Fatalf("marshaling to JSON failed: %v", err)
  15. }
  16. got := string(data)
  17. expected := `{"query":{"match_all":{}}}`
  18. if got != expected {
  19. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  20. }
  21. }
  22. func TestSearchSourceNoFields(t *testing.T) {
  23. matchAllQ := NewMatchAllQuery()
  24. builder := NewSearchSource().Query(matchAllQ).NoFields()
  25. data, err := json.Marshal(builder.Source())
  26. if err != nil {
  27. t.Fatalf("marshaling to JSON failed: %v", err)
  28. }
  29. got := string(data)
  30. expected := `{"fields":[],"query":{"match_all":{}}}`
  31. if got != expected {
  32. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  33. }
  34. }
  35. func TestSearchSourceFields(t *testing.T) {
  36. matchAllQ := NewMatchAllQuery()
  37. builder := NewSearchSource().Query(matchAllQ).Fields("message", "tags")
  38. data, err := json.Marshal(builder.Source())
  39. if err != nil {
  40. t.Fatalf("marshaling to JSON failed: %v", err)
  41. }
  42. got := string(data)
  43. expected := `{"fields":["message","tags"],"query":{"match_all":{}}}`
  44. if got != expected {
  45. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  46. }
  47. }
  48. func TestSearchSourceFetchSourceDisabled(t *testing.T) {
  49. matchAllQ := NewMatchAllQuery()
  50. builder := NewSearchSource().Query(matchAllQ).FetchSource(false)
  51. data, err := json.Marshal(builder.Source())
  52. if err != nil {
  53. t.Fatalf("marshaling to JSON failed: %v", err)
  54. }
  55. got := string(data)
  56. expected := `{"_source":false,"query":{"match_all":{}}}`
  57. if got != expected {
  58. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  59. }
  60. }
  61. func TestSearchSourceFetchSourceByWildcards(t *testing.T) {
  62. matchAllQ := NewMatchAllQuery()
  63. fsc := NewFetchSourceContext(true).Include("obj1.*", "obj2.*").Exclude("*.description")
  64. builder := NewSearchSource().Query(matchAllQ).FetchSourceContext(fsc)
  65. data, err := json.Marshal(builder.Source())
  66. if err != nil {
  67. t.Fatalf("marshaling to JSON failed: %v", err)
  68. }
  69. got := string(data)
  70. expected := `{"_source":{"excludes":["*.description"],"includes":["obj1.*","obj2.*"]},"query":{"match_all":{}}}`
  71. if got != expected {
  72. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  73. }
  74. }
  75. func TestSearchSourceFieldDataFields(t *testing.T) {
  76. matchAllQ := NewMatchAllQuery()
  77. builder := NewSearchSource().Query(matchAllQ).FieldDataFields("test1", "test2")
  78. data, err := json.Marshal(builder.Source())
  79. if err != nil {
  80. t.Fatalf("marshaling to JSON failed: %v", err)
  81. }
  82. got := string(data)
  83. expected := `{"fielddata_fields":["test1","test2"],"query":{"match_all":{}}}`
  84. if got != expected {
  85. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  86. }
  87. }
  88. func TestSearchSourceScriptFields(t *testing.T) {
  89. matchAllQ := NewMatchAllQuery()
  90. sf1 := NewScriptField("test1", "doc['my_field_name'].value * 2", "", nil)
  91. sf2 := NewScriptField("test2", "doc['my_field_name'].value * factor", "", map[string]interface{}{"factor": 3.1415927})
  92. builder := NewSearchSource().Query(matchAllQ).ScriptFields(sf1, sf2)
  93. data, err := json.Marshal(builder.Source())
  94. if err != nil {
  95. t.Fatalf("marshaling to JSON failed: %v", err)
  96. }
  97. got := string(data)
  98. expected := `{"query":{"match_all":{}},"script_fields":{"test1":{"script":"doc['my_field_name'].value * 2"},"test2":{"params":{"factor":3.1415927},"script":"doc['my_field_name'].value * factor"}}}`
  99. if got != expected {
  100. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  101. }
  102. }
  103. func TestSearchSourcePostFilter(t *testing.T) {
  104. matchAllQ := NewMatchAllQuery()
  105. pf := NewTermFilter("tag", "important")
  106. builder := NewSearchSource().Query(matchAllQ).PostFilter(pf)
  107. data, err := json.Marshal(builder.Source())
  108. if err != nil {
  109. t.Fatalf("marshaling to JSON failed: %v", err)
  110. }
  111. got := string(data)
  112. expected := `{"post_filter":{"term":{"tag":"important"}},"query":{"match_all":{}}}`
  113. if got != expected {
  114. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  115. }
  116. }
  117. func TestSearchSourceHighlight(t *testing.T) {
  118. matchAllQ := NewMatchAllQuery()
  119. hl := NewHighlight().Field("content")
  120. builder := NewSearchSource().Query(matchAllQ).Highlight(hl)
  121. data, err := json.Marshal(builder.Source())
  122. if err != nil {
  123. t.Fatalf("marshaling to JSON failed: %v", err)
  124. }
  125. got := string(data)
  126. expected := `{"highlight":{"fields":{"content":{}}},"query":{"match_all":{}}}`
  127. if got != expected {
  128. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  129. }
  130. }
  131. func TestSearchSourceRescoring(t *testing.T) {
  132. matchAllQ := NewMatchAllQuery()
  133. rescorerQuery := NewMatchQuery("field1", "the quick brown fox").Type("phrase").Slop(2)
  134. rescorer := NewQueryRescorer(rescorerQuery)
  135. rescorer = rescorer.QueryWeight(0.7)
  136. rescorer = rescorer.RescoreQueryWeight(1.2)
  137. rescore := NewRescore().WindowSize(50).Rescorer(rescorer)
  138. builder := NewSearchSource().Query(matchAllQ).AddRescore(rescore)
  139. data, err := json.Marshal(builder.Source())
  140. if err != nil {
  141. t.Fatalf("marshaling to JSON failed: %v", err)
  142. }
  143. got := string(data)
  144. expected := `{"query":{"match_all":{}},"rescore":{"query":{"query_weight":0.7,"rescore_query":{"match":{"field1":{"query":"the quick brown fox","slop":2,"type":"phrase"}}},"rescore_query_weight":1.2},"window_size":50}}`
  145. if got != expected {
  146. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  147. }
  148. }
  149. func TestSearchSourceIndexBoost(t *testing.T) {
  150. matchAllQ := NewMatchAllQuery()
  151. builder := NewSearchSource().Query(matchAllQ).IndexBoost("index1", 1.4).IndexBoost("index2", 1.3)
  152. data, err := json.Marshal(builder.Source())
  153. if err != nil {
  154. t.Fatalf("marshaling to JSON failed: %v", err)
  155. }
  156. got := string(data)
  157. expected := `{"indices_boost":{"index1":1.4,"index2":1.3},"query":{"match_all":{}}}`
  158. if got != expected {
  159. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  160. }
  161. }