Skip to content

Commit

Permalink
Update DateHistogram to parameters used in 2.x
Browse files Browse the repository at this point in the history
Several parameters were removed in 2.x, e.g. `preZone` and `preOffset`.
Some were added, e.g. `timeZone`.
  • Loading branch information
olivere committed Dec 22, 2015
1 parent c243240 commit 33747b6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 55 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
// Version is the current version of Elastic.
Version = "3.0.13"
Version = "3.0.14"

// DefaultUrl is the default endpoint of Elasticsearch on the local machine.
// It is used e.g. when initializing a new Client without a specific URL.
Expand Down
85 changes: 33 additions & 52 deletions search_aggs_bucket_date_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,25 @@ type DateHistogramAggregation struct {
subAggregations map[string]Aggregation
meta map[string]interface{}

interval string
order string
orderAsc bool
minDocCount *int64
extendedBoundsMin interface{}
extendedBoundsMax interface{}
preZone string
postZone string
preZoneAdjustLargeInterval *bool
format string
preOffset int64
postOffset int64
factor *float64
interval string
order string
orderAsc bool
minDocCount *int64
extendedBoundsMin interface{}
extendedBoundsMax interface{}
timeZone string
format string
offset string
}

// NewDateHistogramAggregation creates a new DateHistogramAggregation.
func NewDateHistogramAggregation() *DateHistogramAggregation {
return &DateHistogramAggregation{
subAggregations: make(map[string]Aggregation),
}
}

// Field on which the aggregation is processed.
func (a *DateHistogramAggregation) Field(field string) *DateHistogramAggregation {
a.field = field
return a
Expand All @@ -55,6 +53,7 @@ func (a *DateHistogramAggregation) Meta(metaData map[string]interface{}) *DateHi
return a
}

// Interval by which the aggregation gets processed.
// Allowed values are: "year", "quarter", "month", "week", "day",
// "hour", "minute". It also supports time settings like "1.5h"
// (up to "w" for weeks).
Expand Down Expand Up @@ -144,43 +143,37 @@ func (a *DateHistogramAggregation) OrderByAggregationAndMetric(aggName, metric s
return a
}

// MinDocCount sets the minimum document count per bucket.
// Buckets with less documents than this min value will not be returned.
func (a *DateHistogramAggregation) MinDocCount(minDocCount int64) *DateHistogramAggregation {
a.minDocCount = &minDocCount
return a
}

func (a *DateHistogramAggregation) PreZone(preZone string) *DateHistogramAggregation {
a.preZone = preZone
// TimeZone sets the timezone in which to translate dates before computing buckets.
func (a *DateHistogramAggregation) TimeZone(timeZone string) *DateHistogramAggregation {
a.timeZone = timeZone
return a
}

func (a *DateHistogramAggregation) PostZone(postZone string) *DateHistogramAggregation {
a.postZone = postZone
return a
}

func (a *DateHistogramAggregation) PreZoneAdjustLargeInterval(preZoneAdjustLargeInterval bool) *DateHistogramAggregation {
a.preZoneAdjustLargeInterval = &preZoneAdjustLargeInterval
return a
}

func (a *DateHistogramAggregation) PreOffset(preOffset int64) *DateHistogramAggregation {
a.preOffset = preOffset
return a
}

func (a *DateHistogramAggregation) PostOffset(postOffset int64) *DateHistogramAggregation {
a.postOffset = postOffset
// Format sets the format to use for dates.
func (a *DateHistogramAggregation) Format(format string) *DateHistogramAggregation {
a.format = format
return a
}

func (a *DateHistogramAggregation) Factor(factor float64) *DateHistogramAggregation {
a.factor = &factor
// Offset sets the offset of time intervals in the histogram, e.g. "+6h".
func (a *DateHistogramAggregation) Offset(offset string) *DateHistogramAggregation {
a.offset = offset
return a
}

func (a *DateHistogramAggregation) Format(format string) *DateHistogramAggregation {
a.format = format
// ExtendedBounds accepts int, int64, string, or time.Time values.
// In case the lower value in the histogram would be greater than min or the
// upper value would be less than max, empty buckets will be generated.
func (a *DateHistogramAggregation) ExtendedBounds(min, max interface{}) *DateHistogramAggregation {
a.extendedBoundsMin = min
a.extendedBoundsMax = max
return a
}

Expand Down Expand Up @@ -240,23 +233,11 @@ func (a *DateHistogramAggregation) Source() (interface{}, error) {
}
opts["order"] = o
}
if a.preZone != "" {
opts["pre_zone"] = a.preZone
}
if a.postZone != "" {
opts["post_zone"] = a.postZone
}
if a.preZoneAdjustLargeInterval != nil {
opts["pre_zone_adjust_large_interval"] = *a.preZoneAdjustLargeInterval
}
if a.preOffset != 0 {
opts["pre_offset"] = a.preOffset
}
if a.postOffset != 0 {
opts["post_offset"] = a.postOffset
if a.timeZone != "" {
opts["time_zone"] = a.timeZone
}
if a.factor != nil {
opts["factor"] = *a.factor
if a.offset != "" {
opts["offset"] = a.offset
}
if a.format != "" {
opts["format"] = a.format
Expand Down
9 changes: 7 additions & 2 deletions search_aggs_bucket_date_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import (
)

func TestDateHistogramAggregation(t *testing.T) {
agg := NewDateHistogramAggregation().Field("date").Interval("month").Format("YYYY-MM")
agg := NewDateHistogramAggregation().
Field("date").
Interval("month").
Format("YYYY-MM").
TimeZone("UTC").
Offset("+6h")
src, err := agg.Source()
if err != nil {
t.Fatal(err)
Expand All @@ -20,7 +25,7 @@ func TestDateHistogramAggregation(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"date_histogram":{"field":"date","format":"YYYY-MM","interval":"month"}}`
expected := `{"date_histogram":{"field":"date","format":"YYYY-MM","interval":"month","offset":"+6h","time_zone":"UTC"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down

0 comments on commit 33747b6

Please sign in to comment.