search_aggs_bucket_range_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 TestRangeAggregation(t *testing.T) {
  10. agg := NewRangeAggregation().Field("price")
  11. agg = agg.AddRange(nil, 50)
  12. agg = agg.AddRange(50, 100)
  13. agg = agg.AddRange(100, nil)
  14. src, err := agg.Source()
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. data, err := json.Marshal(src)
  19. if err != nil {
  20. t.Fatalf("marshaling to JSON failed: %v", err)
  21. }
  22. got := string(data)
  23. expected := `{"range":{"field":"price","ranges":[{"to":50},{"from":50,"to":100},{"from":100}]}}`
  24. if got != expected {
  25. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  26. }
  27. }
  28. func TestRangeAggregationWithPointers(t *testing.T) {
  29. fifty := 50
  30. hundred := 100
  31. agg := NewRangeAggregation().Field("price")
  32. agg = agg.AddRange(nil, &fifty)
  33. agg = agg.AddRange(fifty, &hundred)
  34. agg = agg.AddRange(hundred, nil)
  35. src, err := agg.Source()
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. data, err := json.Marshal(src)
  40. if err != nil {
  41. t.Fatalf("marshaling to JSON failed: %v", err)
  42. }
  43. got := string(data)
  44. expected := `{"range":{"field":"price","ranges":[{"to":50},{"from":50,"to":100},{"from":100}]}}`
  45. if got != expected {
  46. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  47. }
  48. }
  49. func TestRangeAggregationWithUnbounded(t *testing.T) {
  50. agg := NewRangeAggregation().Field("field_name").
  51. AddUnboundedFrom(50).
  52. AddRange(20, 70).
  53. AddRange(70, 120).
  54. AddUnboundedTo(150)
  55. src, err := agg.Source()
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. data, err := json.Marshal(src)
  60. if err != nil {
  61. t.Fatalf("marshaling to JSON failed: %v", err)
  62. }
  63. got := string(data)
  64. expected := `{"range":{"field":"field_name","ranges":[{"to":50},{"from":20,"to":70},{"from":70,"to":120},{"from":150}]}}`
  65. if got != expected {
  66. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  67. }
  68. }
  69. func TestRangeAggregationWithLtAndCo(t *testing.T) {
  70. agg := NewRangeAggregation().Field("field_name").
  71. Lt(50).
  72. Between(20, 70).
  73. Between(70, 120).
  74. Gt(150)
  75. src, err := agg.Source()
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. data, err := json.Marshal(src)
  80. if err != nil {
  81. t.Fatalf("marshaling to JSON failed: %v", err)
  82. }
  83. got := string(data)
  84. expected := `{"range":{"field":"field_name","ranges":[{"to":50},{"from":20,"to":70},{"from":70,"to":120},{"from":150}]}}`
  85. if got != expected {
  86. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  87. }
  88. }
  89. func TestRangeAggregationWithKeyedFlag(t *testing.T) {
  90. agg := NewRangeAggregation().Field("field_name").
  91. Keyed(true).
  92. Lt(50).
  93. Between(20, 70).
  94. Between(70, 120).
  95. Gt(150)
  96. src, err := agg.Source()
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. data, err := json.Marshal(src)
  101. if err != nil {
  102. t.Fatalf("marshaling to JSON failed: %v", err)
  103. }
  104. got := string(data)
  105. expected := `{"range":{"field":"field_name","keyed":true,"ranges":[{"to":50},{"from":20,"to":70},{"from":70,"to":120},{"from":150}]}}`
  106. if got != expected {
  107. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  108. }
  109. }
  110. func TestRangeAggregationWithKeys(t *testing.T) {
  111. agg := NewRangeAggregation().Field("field_name").
  112. Keyed(true).
  113. LtWithKey("cheap", 50).
  114. BetweenWithKey("affordable", 20, 70).
  115. BetweenWithKey("average", 70, 120).
  116. GtWithKey("expensive", 150)
  117. src, err := agg.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 := `{"range":{"field":"field_name","keyed":true,"ranges":[{"key":"cheap","to":50},{"from":20,"key":"affordable","to":70},{"from":70,"key":"average","to":120},{"from":150,"key":"expensive"}]}}`
  127. if got != expected {
  128. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  129. }
  130. }
  131. func TestRangeAggregationWithMetaData(t *testing.T) {
  132. agg := NewRangeAggregation().Field("price").Meta(map[string]interface{}{"name": "Oliver"})
  133. agg = agg.AddRange(nil, 50)
  134. agg = agg.AddRange(50, 100)
  135. agg = agg.AddRange(100, nil)
  136. src, err := agg.Source()
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. data, err := json.Marshal(src)
  141. if err != nil {
  142. t.Fatalf("marshaling to JSON failed: %v", err)
  143. }
  144. got := string(data)
  145. expected := `{"meta":{"name":"Oliver"},"range":{"field":"price","ranges":[{"to":50},{"from":50,"to":100},{"from":100}]}}`
  146. if got != expected {
  147. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  148. }
  149. }
  150. func TestRangeAggregationWithMissing(t *testing.T) {
  151. agg := NewRangeAggregation().Field("price").Missing(0)
  152. agg = agg.AddRange(nil, 50)
  153. agg = agg.AddRange(50, 100)
  154. agg = agg.AddRange(100, nil)
  155. src, err := agg.Source()
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. data, err := json.Marshal(src)
  160. if err != nil {
  161. t.Fatalf("marshaling to JSON failed: %v", err)
  162. }
  163. got := string(data)
  164. expected := `{"range":{"field":"price","missing":0,"ranges":[{"to":50},{"from":50,"to":100},{"from":100}]}}`
  165. if got != expected {
  166. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  167. }
  168. }