Skip to content

Commit

Permalink
Add offset option to histogram aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
olivere committed Aug 28, 2015
1 parent a66f919 commit 86dfce5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ The combination of `Metric(string)` and `Metrics(...string)` has been replaced b

Services generally return a typed response from a `Do` func. Those structs are exported so that they can be passed around in your own application. In Elastic 3.0 however, we changed that (most) sub-structs are now unexported, meaning: You can only pass around the whole response, not sub-structures of it. This makes it easier for restructuring responses according to the Elasticsearch API. See [`ClusterStateResponse`](https://github.com/olivere/elastic/blob/release-branch.v3/cluster_state.go#L182) as an example.

## Add offset to Histogram aggregation

Histogram aggregations now have an [offset](https://github.com/elastic/elasticsearch/pull/9505) option.

## Services

### REST API specification
Expand Down
15 changes: 15 additions & 0 deletions search_aggs_bucket_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type HistogramAggregation struct {
minDocCount *int64
extendedBoundsMin *int64
extendedBoundsMax *int64
offset *int64
}

func NewHistogramAggregation() *HistogramAggregation {
Expand Down Expand Up @@ -141,6 +142,12 @@ func (a *HistogramAggregation) MinDocCount(minDocCount int64) *HistogramAggregat
return a
}

func (a *HistogramAggregation) ExtendedBounds(min, max int64) *HistogramAggregation {
a.extendedBoundsMin = &min
a.extendedBoundsMax = &max
return a
}

func (a *HistogramAggregation) ExtendedBoundsMin(min int64) *HistogramAggregation {
a.extendedBoundsMin = &min
return a
Expand All @@ -151,6 +158,11 @@ func (a *HistogramAggregation) ExtendedBoundsMax(max int64) *HistogramAggregatio
return a
}

func (a *HistogramAggregation) Offset(offset int64) *HistogramAggregation {
a.offset = &offset
return a
}

func (a *HistogramAggregation) Source() (interface{}, error) {
// Example:
// {
Expand Down Expand Up @@ -192,6 +204,9 @@ func (a *HistogramAggregation) Source() (interface{}, error) {
}
opts["order"] = o
}
if a.offset != nil {
opts["offset"] = *a.offset
}
if a.minDocCount != nil {
opts["min_doc_count"] = *a.minDocCount
}
Expand Down
4 changes: 2 additions & 2 deletions search_aggs_bucket_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestHistogramAggregation(t *testing.T) {
}

func TestHistogramAggregationWithMetaData(t *testing.T) {
agg := NewHistogramAggregation().Field("price").Interval(50).Meta(map[string]interface{}{"name": "Oliver"})
agg := NewHistogramAggregation().Field("price").Offset(10).Interval(50).Meta(map[string]interface{}{"name": "Oliver"})
src, err := agg.Source()
if err != nil {
t.Fatal(err)
Expand All @@ -37,7 +37,7 @@ func TestHistogramAggregationWithMetaData(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"histogram":{"field":"price","interval":50},"meta":{"name":"Oliver"}}`
expected := `{"histogram":{"field":"price","interval":50,"offset":10},"meta":{"name":"Oliver"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down

0 comments on commit 86dfce5

Please sign in to comment.