sort_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 TestSortInfo(t *testing.T) {
  10. builder := SortInfo{Field: "grade", Ascending: false}
  11. src, err := builder.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"grade":{"order":"desc"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestSortInfoComplex(t *testing.T) {
  26. builder := SortInfo{
  27. Field: "price",
  28. Ascending: false,
  29. Missing: "_last",
  30. SortMode: "avg",
  31. NestedFilter: NewTermQuery("product.color", "blue"),
  32. NestedPath: "variant",
  33. }
  34. src, err := builder.Source()
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. data, err := json.Marshal(src)
  39. if err != nil {
  40. t.Fatalf("marshaling to JSON failed: %v", err)
  41. }
  42. got := string(data)
  43. expected := `{"price":{"missing":"_last","mode":"avg","nested_filter":{"term":{"product.color":"blue"}},"nested_path":"variant","order":"desc"}}`
  44. if got != expected {
  45. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  46. }
  47. }
  48. func TestScoreSort(t *testing.T) {
  49. builder := NewScoreSort()
  50. if builder.ascending != false {
  51. t.Error("expected score sorter to be ascending by default")
  52. }
  53. src, err := builder.Source()
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. data, err := json.Marshal(src)
  58. if err != nil {
  59. t.Fatalf("marshaling to JSON failed: %v", err)
  60. }
  61. got := string(data)
  62. expected := `{"_score":{"order":"desc"}}` // ScoreSort is "desc" by default
  63. if got != expected {
  64. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  65. }
  66. }
  67. func TestScoreSortOrderAscending(t *testing.T) {
  68. builder := NewScoreSort().Asc()
  69. src, err := builder.Source()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. data, err := json.Marshal(src)
  74. if err != nil {
  75. t.Fatalf("marshaling to JSON failed: %v", err)
  76. }
  77. got := string(data)
  78. expected := `{"_score":{"order":"asc"}}`
  79. if got != expected {
  80. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  81. }
  82. }
  83. func TestScoreSortOrderDescending(t *testing.T) {
  84. builder := NewScoreSort().Desc()
  85. src, err := builder.Source()
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. data, err := json.Marshal(src)
  90. if err != nil {
  91. t.Fatalf("marshaling to JSON failed: %v", err)
  92. }
  93. got := string(data)
  94. expected := `{"_score":{"order":"desc"}}`
  95. if got != expected {
  96. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  97. }
  98. }
  99. func TestFieldSort(t *testing.T) {
  100. builder := NewFieldSort("grade")
  101. src, err := builder.Source()
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. data, err := json.Marshal(src)
  106. if err != nil {
  107. t.Fatalf("marshaling to JSON failed: %v", err)
  108. }
  109. got := string(data)
  110. expected := `{"grade":{"order":"asc"}}`
  111. if got != expected {
  112. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  113. }
  114. }
  115. func TestFieldSortOrderDesc(t *testing.T) {
  116. builder := NewFieldSort("grade").Desc()
  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 := `{"grade":{"order":"desc"}}`
  127. if got != expected {
  128. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  129. }
  130. }
  131. func TestFieldSortComplex(t *testing.T) {
  132. builder := NewFieldSort("price").Desc().
  133. SortMode("avg").
  134. Missing("_last").
  135. UnmappedType("product").
  136. NestedFilter(NewTermQuery("product.color", "blue")).
  137. NestedPath("variant")
  138. src, err := builder.Source()
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. data, err := json.Marshal(src)
  143. if err != nil {
  144. t.Fatalf("marshaling to JSON failed: %v", err)
  145. }
  146. got := string(data)
  147. expected := `{"price":{"missing":"_last","mode":"avg","nested_filter":{"term":{"product.color":"blue"}},"nested_path":"variant","order":"desc","unmapped_type":"product"}}`
  148. if got != expected {
  149. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  150. }
  151. }
  152. func TestGeoDistanceSort(t *testing.T) {
  153. builder := NewGeoDistanceSort("pin.location").
  154. Point(-70, 40).
  155. Order(true).
  156. Unit("km").
  157. SortMode("min").
  158. GeoDistance("plane")
  159. src, err := builder.Source()
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. data, err := json.Marshal(src)
  164. if err != nil {
  165. t.Fatalf("marshaling to JSON failed: %v", err)
  166. }
  167. got := string(data)
  168. expected := `{"_geo_distance":{"distance_type":"plane","mode":"min","order":"asc","pin.location":[{"lat":-70,"lon":40}],"unit":"km"}}`
  169. if got != expected {
  170. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  171. }
  172. }
  173. func TestGeoDistanceSortOrderDesc(t *testing.T) {
  174. builder := NewGeoDistanceSort("pin.location").
  175. Point(-70, 40).
  176. Unit("km").
  177. SortMode("min").
  178. GeoDistance("arc").
  179. Desc()
  180. src, err := builder.Source()
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. data, err := json.Marshal(src)
  185. if err != nil {
  186. t.Fatalf("marshaling to JSON failed: %v", err)
  187. }
  188. got := string(data)
  189. expected := `{"_geo_distance":{"distance_type":"arc","mode":"min","order":"desc","pin.location":[{"lat":-70,"lon":40}],"unit":"km"}}`
  190. if got != expected {
  191. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  192. }
  193. }
  194. func TestScriptSort(t *testing.T) {
  195. builder := NewScriptSort(NewScript("doc['field_name'].value * factor").Param("factor", 1.1), "number").Order(true)
  196. src, err := builder.Source()
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. data, err := json.Marshal(src)
  201. if err != nil {
  202. t.Fatalf("marshaling to JSON failed: %v", err)
  203. }
  204. got := string(data)
  205. expected := `{"_script":{"order":"asc","script":{"inline":"doc['field_name'].value * factor","params":{"factor":1.1}},"type":"number"}}`
  206. if got != expected {
  207. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  208. }
  209. }
  210. func TestScriptSortOrderDesc(t *testing.T) {
  211. builder := NewScriptSort(NewScript("doc['field_name'].value * factor").Param("factor", 1.1), "number").Desc()
  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 := `{"_script":{"order":"desc","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. }