forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_aggs_pipeline_bucket_sort.go
119 lines (101 loc) · 3.48 KB
/
search_aggs_pipeline_bucket_sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
// BucketSortAggregation parent pipeline aggregation which sorts the buckets
// of its parent multi-bucket aggregation. Zero or more sort fields may be
// specified together with the corresponding sort order. Each bucket may be
// sorted based on its _key, _count or its sub-aggregations. In addition,
// parameters from and size may be set in order to truncate the result buckets.
//
// For more details, see
// https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-pipeline-bucket-sort-aggregation.html
type BucketSortAggregation struct {
sorters []Sorter
from int
size int
gapPolicy string
meta map[string]interface{}
}
// NewBucketSortAggregation creates and initializes a new BucketSortAggregation.
func NewBucketSortAggregation() *BucketSortAggregation {
return &BucketSortAggregation{
size: -1,
}
}
// Sort adds a sort order to the list of sorters.
func (a *BucketSortAggregation) Sort(field string, ascending bool) *BucketSortAggregation {
a.sorters = append(a.sorters, SortInfo{Field: field, Ascending: ascending})
return a
}
// SortWithInfo adds a SortInfo to the list of sorters.
func (a *BucketSortAggregation) SortWithInfo(info SortInfo) *BucketSortAggregation {
a.sorters = append(a.sorters, info)
return a
}
// From adds the "from" parameter to the aggregation.
func (a *BucketSortAggregation) From(from int) *BucketSortAggregation {
a.from = from
return a
}
// Size adds the "size" parameter to the aggregation.
func (a *BucketSortAggregation) Size(size int) *BucketSortAggregation {
a.size = size
return a
}
// GapPolicy defines what should be done when a gap in the series is discovered.
// Valid values include "insert_zeros" or "skip". Default is "skip".
func (a *BucketSortAggregation) GapPolicy(gapPolicy string) *BucketSortAggregation {
a.gapPolicy = gapPolicy
return a
}
// GapInsertZeros inserts zeros for gaps in the series.
func (a *BucketSortAggregation) GapInsertZeros() *BucketSortAggregation {
a.gapPolicy = "insert_zeros"
return a
}
// GapSkip skips gaps in the series.
func (a *BucketSortAggregation) GapSkip() *BucketSortAggregation {
a.gapPolicy = "skip"
return a
}
// Meta sets the meta data in the aggregation.
// Although metadata is supported for this aggregation by Elasticsearch, it's important to
// note that there's no use to it because this aggregation does not include new data in the
// response. It merely reorders parent buckets.
func (a *BucketSortAggregation) Meta(meta map[string]interface{}) *BucketSortAggregation {
a.meta = meta
return a
}
// Source returns the a JSON-serializable interface.
func (a *BucketSortAggregation) Source() (interface{}, error) {
source := make(map[string]interface{})
params := make(map[string]interface{})
source["bucket_sort"] = params
if a.from != 0 {
params["from"] = a.from
}
if a.size != -1 {
params["size"] = a.size
}
if a.gapPolicy != "" {
params["gap_policy"] = a.gapPolicy
}
// Parses sorters to JSON-serializable interface.
if len(a.sorters) > 0 {
sorters := make([]interface{}, len(a.sorters))
params["sort"] = sorters
for idx, sorter := range a.sorters {
src, err := sorter.Source()
if err != nil {
return nil, err
}
sorters[idx] = src
}
}
// Add metadata if available.
if len(a.meta) > 0 {
source["meta"] = a.meta
}
return source, nil
}