Skip to content

Commit dbb16ba

Browse files
committed
Remove inconsistencies between range aggregations
Some aggregations supported pointers, some did not. This should now be aligned and behaviour should be the same across implementations.
1 parent fecaf71 commit dbb16ba

7 files changed

+98
-0
lines changed

CONTRIBUTORS

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Alex [@akotlar](https://github.com/akotlar)
1515
Alexandre Olivier [@aliphen](https://github.com/aliphen)
1616
Alexey Sharov [@nizsheanez](https://github.com/nizsheanez)
1717
AndreKR [@AndreKR](https://github.com/AndreKR)
18+
André Bierlein [@ligustah](https://github.com/ligustah)
1819
Andrew Dunham [@andrew-d](https://github.com/andrew-d)
1920
Andrew Gaul [@andrewgaul](https://github.com/andrewgaul)
2021
Arquivei [@arquivei](https://github.com/arquivei)

search_aggs_bucket_date_range.go

+12
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,32 @@ func (a *DateRangeAggregation) Source() (interface{}, error) {
192192
switch from := ent.From.(type) {
193193
case int, int16, int32, int64, float32, float64:
194194
r["from"] = from
195+
case *int, *int16, *int32, *int64, *float32, *float64:
196+
r["from"] = from
195197
case time.Time:
196198
r["from"] = from.Format(time.RFC3339)
199+
case *time.Time:
200+
r["from"] = from.Format(time.RFC3339)
197201
case string:
198202
r["from"] = from
203+
case *string:
204+
r["from"] = from
199205
}
200206
}
201207
if ent.To != nil {
202208
switch to := ent.To.(type) {
203209
case int, int16, int32, int64, float32, float64:
204210
r["to"] = to
211+
case *int, *int16, *int32, *int64, *float32, *float64:
212+
r["to"] = to
205213
case time.Time:
206214
r["to"] = to.Format(time.RFC3339)
215+
case *time.Time:
216+
r["to"] = to.Format(time.RFC3339)
207217
case string:
208218
r["to"] = to
219+
case *string:
220+
r["to"] = to
209221
}
210222
}
211223
ranges = append(ranges, r)

search_aggs_bucket_date_range_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ func TestDateRangeAggregation(t *testing.T) {
2929
}
3030
}
3131

32+
func TestDateRangeAggregationWithPointers(t *testing.T) {
33+
d1 := "2012-12-31"
34+
d2 := "2013-01-01"
35+
d3 := "2013-12-31"
36+
d4 := "2014-01-01"
37+
38+
agg := NewDateRangeAggregation().Field("created_at")
39+
agg = agg.AddRange(nil, &d1)
40+
agg = agg.AddRange(d2, &d3)
41+
agg = agg.AddRange(d4, nil)
42+
src, err := agg.Source()
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
data, err := json.Marshal(src)
47+
if err != nil {
48+
t.Fatalf("marshaling to JSON failed: %v", err)
49+
}
50+
got := string(data)
51+
expected := `{"date_range":{"field":"created_at","ranges":[{"to":"2012-12-31"},{"from":"2013-01-01","to":"2013-12-31"},{"from":"2014-01-01"}]}}`
52+
if got != expected {
53+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
54+
}
55+
}
56+
3257
func TestDateRangeAggregationWithUnbounded(t *testing.T) {
3358
agg := NewDateRangeAggregation().Field("created_at").
3459
AddUnboundedFrom("2012-12-31").

search_aggs_bucket_geo_distance.go

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ func (a *GeoDistanceAggregation) Source() (interface{}, error) {
156156
r["from"] = from
157157
case string:
158158
r["from"] = from
159+
case *string:
160+
r["from"] = from
159161
}
160162
}
161163
if ent.To != nil {
@@ -166,6 +168,8 @@ func (a *GeoDistanceAggregation) Source() (interface{}, error) {
166168
r["to"] = to
167169
case string:
168170
r["to"] = to
171+
case *string:
172+
r["to"] = to
169173
}
170174
}
171175
ranges = append(ranges, r)

search_aggs_bucket_geo_distance_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ func TestGeoDistanceAggregation(t *testing.T) {
2929
}
3030
}
3131

32+
func TestGeoDistanceAggregationWithPointers(t *testing.T) {
33+
hundred := 100
34+
threeHundred := 300
35+
agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894")
36+
agg = agg.AddRange(nil, &hundred)
37+
agg = agg.AddRange(hundred, &threeHundred)
38+
agg = agg.AddRange(threeHundred, nil)
39+
src, err := agg.Source()
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
data, err := json.Marshal(src)
44+
if err != nil {
45+
t.Fatalf("marshaling to JSON failed: %v", err)
46+
}
47+
got := string(data)
48+
expected := `{"geo_distance":{"field":"location","origin":"52.3760, 4.894","ranges":[{"to":100},{"from":100,"to":300},{"from":300}]}}`
49+
if got != expected {
50+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
51+
}
52+
}
53+
3254
func TestGeoDistanceAggregationWithUnbounded(t *testing.T) {
3355
agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894")
3456
agg = agg.AddUnboundedFrom(100)

search_aggs_bucket_range.go

+12
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,32 @@ func (a *RangeAggregation) Source() (interface{}, error) {
191191
switch from := ent.From.(type) {
192192
case int, int16, int32, int64, float32, float64:
193193
r["from"] = from
194+
case *int, *int16, *int32, *int64, *float32, *float64:
195+
r["from"] = from
194196
case time.Time:
195197
r["from"] = from.Format(time.RFC3339)
198+
case *time.Time:
199+
r["from"] = from.Format(time.RFC3339)
196200
case string:
197201
r["from"] = from
202+
case *string:
203+
r["from"] = from
198204
}
199205
}
200206
if ent.To != nil {
201207
switch to := ent.To.(type) {
202208
case int, int16, int32, int64, float32, float64:
203209
r["to"] = to
210+
case *int, *int16, *int32, *int64, *float32, *float64:
211+
r["to"] = to
204212
case time.Time:
205213
r["to"] = to.Format(time.RFC3339)
214+
case *time.Time:
215+
r["to"] = to.Format(time.RFC3339)
206216
case string:
207217
r["to"] = to
218+
case *string:
219+
r["to"] = to
208220
}
209221
}
210222
ranges = append(ranges, r)

search_aggs_bucket_range_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ func TestRangeAggregation(t *testing.T) {
2929
}
3030
}
3131

32+
func TestRangeAggregationWithPointers(t *testing.T) {
33+
fifty := 50
34+
hundred := 100
35+
agg := NewRangeAggregation().Field("price")
36+
agg = agg.AddRange(nil, &fifty)
37+
agg = agg.AddRange(fifty, &hundred)
38+
agg = agg.AddRange(hundred, nil)
39+
src, err := agg.Source()
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
data, err := json.Marshal(src)
44+
if err != nil {
45+
t.Fatalf("marshaling to JSON failed: %v", err)
46+
}
47+
got := string(data)
48+
expected := `{"range":{"field":"price","ranges":[{"to":50},{"from":50,"to":100},{"from":100}]}}`
49+
if got != expected {
50+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
51+
}
52+
}
53+
3254
func TestRangeAggregationWithUnbounded(t *testing.T) {
3355
agg := NewRangeAggregation().Field("field_name").
3456
AddUnboundedFrom(50).

0 commit comments

Comments
 (0)