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