Skip to content

Commit

Permalink
Remove inconsistencies between range aggregations
Browse files Browse the repository at this point in the history
Some aggregations supported pointers, some did not. This should now be
aligned and behaviour should be the same across implementations.
  • Loading branch information
olivere committed Jul 18, 2017
1 parent fecaf71 commit dbb16ba
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Alex [@akotlar](https://github.com/akotlar)
Alexandre Olivier [@aliphen](https://github.com/aliphen)
Alexey Sharov [@nizsheanez](https://github.com/nizsheanez)
AndreKR [@AndreKR](https://github.com/AndreKR)
André Bierlein [@ligustah](https://github.com/ligustah)
Andrew Dunham [@andrew-d](https://github.com/andrew-d)
Andrew Gaul [@andrewgaul](https://github.com/andrewgaul)
Arquivei [@arquivei](https://github.com/arquivei)
Expand Down
12 changes: 12 additions & 0 deletions search_aggs_bucket_date_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,32 @@ func (a *DateRangeAggregation) Source() (interface{}, error) {
switch from := ent.From.(type) {
case int, int16, int32, int64, float32, float64:
r["from"] = from
case *int, *int16, *int32, *int64, *float32, *float64:
r["from"] = from
case time.Time:
r["from"] = from.Format(time.RFC3339)
case *time.Time:
r["from"] = from.Format(time.RFC3339)
case string:
r["from"] = from
case *string:
r["from"] = from
}
}
if ent.To != nil {
switch to := ent.To.(type) {
case int, int16, int32, int64, float32, float64:
r["to"] = to
case *int, *int16, *int32, *int64, *float32, *float64:
r["to"] = to
case time.Time:
r["to"] = to.Format(time.RFC3339)
case *time.Time:
r["to"] = to.Format(time.RFC3339)
case string:
r["to"] = to
case *string:
r["to"] = to
}
}
ranges = append(ranges, r)
Expand Down
25 changes: 25 additions & 0 deletions search_aggs_bucket_date_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ func TestDateRangeAggregation(t *testing.T) {
}
}

func TestDateRangeAggregationWithPointers(t *testing.T) {
d1 := "2012-12-31"
d2 := "2013-01-01"
d3 := "2013-12-31"
d4 := "2014-01-01"

agg := NewDateRangeAggregation().Field("created_at")
agg = agg.AddRange(nil, &d1)
agg = agg.AddRange(d2, &d3)
agg = agg.AddRange(d4, nil)
src, err := agg.Source()
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"date_range":{"field":"created_at","ranges":[{"to":"2012-12-31"},{"from":"2013-01-01","to":"2013-12-31"},{"from":"2014-01-01"}]}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestDateRangeAggregationWithUnbounded(t *testing.T) {
agg := NewDateRangeAggregation().Field("created_at").
AddUnboundedFrom("2012-12-31").
Expand Down
4 changes: 4 additions & 0 deletions search_aggs_bucket_geo_distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func (a *GeoDistanceAggregation) Source() (interface{}, error) {
r["from"] = from
case string:
r["from"] = from
case *string:
r["from"] = from
}
}
if ent.To != nil {
Expand All @@ -166,6 +168,8 @@ func (a *GeoDistanceAggregation) Source() (interface{}, error) {
r["to"] = to
case string:
r["to"] = to
case *string:
r["to"] = to
}
}
ranges = append(ranges, r)
Expand Down
22 changes: 22 additions & 0 deletions search_aggs_bucket_geo_distance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ func TestGeoDistanceAggregation(t *testing.T) {
}
}

func TestGeoDistanceAggregationWithPointers(t *testing.T) {
hundred := 100
threeHundred := 300
agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894")
agg = agg.AddRange(nil, &hundred)
agg = agg.AddRange(hundred, &threeHundred)
agg = agg.AddRange(threeHundred, nil)
src, err := agg.Source()
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"geo_distance":{"field":"location","origin":"52.3760, 4.894","ranges":[{"to":100},{"from":100,"to":300},{"from":300}]}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestGeoDistanceAggregationWithUnbounded(t *testing.T) {
agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894")
agg = agg.AddUnboundedFrom(100)
Expand Down
12 changes: 12 additions & 0 deletions search_aggs_bucket_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,32 @@ func (a *RangeAggregation) Source() (interface{}, error) {
switch from := ent.From.(type) {
case int, int16, int32, int64, float32, float64:
r["from"] = from
case *int, *int16, *int32, *int64, *float32, *float64:
r["from"] = from
case time.Time:
r["from"] = from.Format(time.RFC3339)
case *time.Time:
r["from"] = from.Format(time.RFC3339)
case string:
r["from"] = from
case *string:
r["from"] = from
}
}
if ent.To != nil {
switch to := ent.To.(type) {
case int, int16, int32, int64, float32, float64:
r["to"] = to
case *int, *int16, *int32, *int64, *float32, *float64:
r["to"] = to
case time.Time:
r["to"] = to.Format(time.RFC3339)
case *time.Time:
r["to"] = to.Format(time.RFC3339)
case string:
r["to"] = to
case *string:
r["to"] = to
}
}
ranges = append(ranges, r)
Expand Down
22 changes: 22 additions & 0 deletions search_aggs_bucket_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ func TestRangeAggregation(t *testing.T) {
}
}

func TestRangeAggregationWithPointers(t *testing.T) {
fifty := 50
hundred := 100
agg := NewRangeAggregation().Field("price")
agg = agg.AddRange(nil, &fifty)
agg = agg.AddRange(fifty, &hundred)
agg = agg.AddRange(hundred, nil)
src, err := agg.Source()
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"range":{"field":"price","ranges":[{"to":50},{"from":50,"to":100},{"from":100}]}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestRangeAggregationWithUnbounded(t *testing.T) {
agg := NewRangeAggregation().Field("field_name").
AddUnboundedFrom(50).
Expand Down

0 comments on commit dbb16ba

Please sign in to comment.