|
| 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 | + |
| 5 | +package elastic |
| 6 | + |
| 7 | +// AutoDateHistogramAggregation is a multi-bucket aggregation similar to the |
| 8 | +// histogram except it can only be applied on date values, and the buckets num can bin pointed. |
| 9 | +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-aggregations-bucket-autodatehistogram-aggregation.html |
| 10 | +type AutoDateHistogramAggregation struct { |
| 11 | + field string |
| 12 | + script *Script |
| 13 | + missing interface{} |
| 14 | + subAggregations map[string]Aggregation |
| 15 | + meta map[string]interface{} |
| 16 | + |
| 17 | + buckets int |
| 18 | + minDocCount *int64 |
| 19 | + timeZone string |
| 20 | + format string |
| 21 | + minimumInterval string |
| 22 | +} |
| 23 | + |
| 24 | +// NewAutoDateHistogramAggregation creates a new AutoDateHistogramAggregation. |
| 25 | +func NewAutoDateHistogramAggregation() *AutoDateHistogramAggregation { |
| 26 | + return &AutoDateHistogramAggregation{ |
| 27 | + subAggregations: make(map[string]Aggregation), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Field on which the aggregation is processed. |
| 32 | +func (a *AutoDateHistogramAggregation) Field(field string) *AutoDateHistogramAggregation { |
| 33 | + a.field = field |
| 34 | + return a |
| 35 | +} |
| 36 | + |
| 37 | +// Script on which th |
| 38 | +func (a *AutoDateHistogramAggregation) Script(script *Script) *AutoDateHistogramAggregation { |
| 39 | + a.script = script |
| 40 | + return a |
| 41 | +} |
| 42 | + |
| 43 | +// Missing configures the value to use when documents miss a value. |
| 44 | +func (a *AutoDateHistogramAggregation) Missing(missing interface{}) *AutoDateHistogramAggregation { |
| 45 | + a.missing = missing |
| 46 | + return a |
| 47 | +} |
| 48 | + |
| 49 | +// SubAggregation sub aggregation |
| 50 | +func (a *AutoDateHistogramAggregation) SubAggregation(name string, subAggregation Aggregation) *AutoDateHistogramAggregation { |
| 51 | + a.subAggregations[name] = subAggregation |
| 52 | + return a |
| 53 | +} |
| 54 | + |
| 55 | +// Meta sets the meta data to be included in the aggregation response. |
| 56 | +func (a *AutoDateHistogramAggregation) Meta(metaData map[string]interface{}) *AutoDateHistogramAggregation { |
| 57 | + a.meta = metaData |
| 58 | + return a |
| 59 | +} |
| 60 | + |
| 61 | +// Buckets buckets num by which the aggregation gets processed. |
| 62 | +func (a *AutoDateHistogramAggregation) Buckets(buckets int) *AutoDateHistogramAggregation { |
| 63 | + a.buckets = buckets |
| 64 | + return a |
| 65 | +} |
| 66 | + |
| 67 | +// MinDocCount sets the minimum document count per bucket. |
| 68 | +// Buckets with less documents than this min value will not be returned. |
| 69 | +func (a *AutoDateHistogramAggregation) MinDocCount(minDocCount int64) *AutoDateHistogramAggregation { |
| 70 | + a.minDocCount = &minDocCount |
| 71 | + return a |
| 72 | +} |
| 73 | + |
| 74 | +// TimeZone sets the timezone in which to translate dates before computing buckets. |
| 75 | +func (a *AutoDateHistogramAggregation) TimeZone(timeZone string) *AutoDateHistogramAggregation { |
| 76 | + a.timeZone = timeZone |
| 77 | + return a |
| 78 | +} |
| 79 | + |
| 80 | +// Format sets the format to use for dates. |
| 81 | +func (a *AutoDateHistogramAggregation) Format(format string) *AutoDateHistogramAggregation { |
| 82 | + a.format = format |
| 83 | + return a |
| 84 | +} |
| 85 | + |
| 86 | +// MinimumInterval accepted units for minimum_interval are: year/month/day/hour/minute/second |
| 87 | +func (a *AutoDateHistogramAggregation) MinimumInterval(interval string) *AutoDateHistogramAggregation { |
| 88 | + a.minimumInterval = interval |
| 89 | + return a |
| 90 | +} |
| 91 | + |
| 92 | +// Source source for AutoDateHistogramAggregation |
| 93 | +func (a *AutoDateHistogramAggregation) Source() (interface{}, error) { |
| 94 | + // Example: |
| 95 | + // { |
| 96 | + // "aggs" : { |
| 97 | + // "articles_over_time" : { |
| 98 | + // "auto_date_histogram" : { |
| 99 | + // "field" : "date", |
| 100 | + // "buckets" : 10 |
| 101 | + // } |
| 102 | + // } |
| 103 | + // } |
| 104 | + // } |
| 105 | + // |
| 106 | + // This method returns only the { "auto_date_histogram" : { ... } } part. |
| 107 | + |
| 108 | + source := make(map[string]interface{}) |
| 109 | + opts := make(map[string]interface{}) |
| 110 | + source["auto_date_histogram"] = opts |
| 111 | + |
| 112 | + // ValuesSourceAggregationBuilder |
| 113 | + if a.field != "" { |
| 114 | + opts["field"] = a.field |
| 115 | + } |
| 116 | + if a.script != nil { |
| 117 | + src, err := a.script.Source() |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + opts["script"] = src |
| 122 | + } |
| 123 | + if a.missing != nil { |
| 124 | + opts["missing"] = a.missing |
| 125 | + } |
| 126 | + |
| 127 | + if a.buckets > 0 { |
| 128 | + opts["buckets"] = a.buckets |
| 129 | + } else { |
| 130 | + opts["buckets"] = 10 |
| 131 | + } |
| 132 | + |
| 133 | + if a.minDocCount != nil { |
| 134 | + opts["min_doc_count"] = *a.minDocCount |
| 135 | + } |
| 136 | + if a.timeZone != "" { |
| 137 | + opts["time_zone"] = a.timeZone |
| 138 | + } |
| 139 | + if a.format != "" { |
| 140 | + opts["format"] = a.format |
| 141 | + } |
| 142 | + if a.minimumInterval != "" { |
| 143 | + opts["minimum_interval"] = a.minimumInterval |
| 144 | + } |
| 145 | + |
| 146 | + // AggregationBuilder (SubAggregations) |
| 147 | + if len(a.subAggregations) > 0 { |
| 148 | + aggsMap := make(map[string]interface{}) |
| 149 | + source["aggregations"] = aggsMap |
| 150 | + for name, aggregate := range a.subAggregations { |
| 151 | + src, err := aggregate.Source() |
| 152 | + if err != nil { |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + aggsMap[name] = src |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Add Meta data if available |
| 160 | + if len(a.meta) > 0 { |
| 161 | + source["meta"] = a.meta |
| 162 | + } |
| 163 | + |
| 164 | + return source, nil |
| 165 | +} |
0 commit comments