/
group_by_histogram.go
167 lines (139 loc) · 3.78 KB
/
group_by_histogram.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package search
import (
"errors"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore/otsprotocol"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore/search/model"
"github.com/golang/protobuf/proto"
)
type GroupByHistogram struct {
GroupByName string
Field string
Interval interface{}
Missing interface{}
MinDocCount *int64
FieldRange model.FiledRange
Sorters []GroupBySorter
SubAggList []Aggregation
SubGroupByList []GroupBy
}
func (g *GroupByHistogram) GetName() string {
return g.GroupByName
}
func (g *GroupByHistogram) GetType() GroupByType {
return GroupByHistogramType
}
func (g *GroupByHistogram) GetField() string {
return g.Field
}
func (g *GroupByHistogram) Serialize() ([]byte, error) {
pbGroupBy := &otsprotocol.GroupByHistogram{}
pbGroupBy.FieldName = &g.Field
pbGroupBy.MinDocCount = g.MinDocCount
if g.Missing != nil {
vt, err := ToVariantValue(g.Missing)
if err != nil {
return nil, err
}
pbGroupBy.Missing = []byte(vt)
}
if g.Interval != nil {
vt, err := ToVariantValue(g.Interval)
if err != nil {
return nil, err
}
pbGroupBy.Interval = []byte(vt)
}
pbGroupBy.FieldRange = &otsprotocol.FieldRange{}
if g.FieldRange.Max != nil {
vt, err := ToVariantValue(g.FieldRange.Max)
if err != nil {
return nil, err
}
pbGroupBy.FieldRange.Max = []byte(vt)
}
if g.FieldRange.Min != nil {
vt, err := ToVariantValue(g.FieldRange.Min)
if err != nil {
return nil, err
}
pbGroupBy.FieldRange.Min = []byte(vt)
}
//sorters
var pbGroupBySorters []*otsprotocol.GroupBySorter
for _, sorter := range g.Sorters {
pbGroupBySorter, err := sorter.ProtoBuffer()
if err != nil {
return nil, errors.New("invalid group by sorter:" + err.Error())
}
pbGroupBySorters = append(pbGroupBySorters, pbGroupBySorter)
}
pbGroupBySort := otsprotocol.GroupBySort{}
pbGroupBySort.Sorters = pbGroupBySorters
pbGroupBy.Sort = &pbGroupBySort
//sub aggs
subGroupBys, err := BuildPBForGroupBys(g.SubGroupByList)
if err != nil {
return nil, err
}
if subGroupBys != nil {
pbGroupBy.SubGroupBys = subGroupBys
}
//sub groupbys
subAggs, err := BuildPBForAggregations(g.SubAggList)
if err != nil {
return nil, err
}
if subAggs != nil {
pbGroupBy.SubAggs = subAggs
}
data, err := proto.Marshal(pbGroupBy)
return data, err
}
func (g *GroupByHistogram) ProtoBuffer() (*otsprotocol.GroupBy, error) {
return BuildPBForGroupBy(g)
}
func (g *GroupByHistogram) SubAggregations(subAggregations ...Aggregation) *GroupByHistogram {
g.SubAggList = subAggregations
return g
}
func (g *GroupByHistogram) SubAggregation(subAggregation Aggregation) *GroupByHistogram {
g.SubAggList = append(g.SubAggList, subAggregation)
return g
}
func (g *GroupByHistogram) SubGroupBys(subGroupBys ...GroupBy) *GroupByHistogram {
g.SubGroupByList = subGroupBys
return g
}
func (g *GroupByHistogram) SubGroupBy(subGroupBy GroupBy) *GroupByHistogram {
g.SubGroupByList = append(g.SubGroupByList, subGroupBy)
return g
}
func (g *GroupByHistogram) SetName(name string) *GroupByHistogram {
g.GroupByName = name
return g
}
func (g *GroupByHistogram) SetMissing(missing interface{}) *GroupByHistogram {
g.Missing = missing
return g
}
func (g *GroupByHistogram) SetMinDocCount(minDocCount int64) *GroupByHistogram {
g.MinDocCount = &minDocCount
return g
}
func (g *GroupByHistogram) SetInterval(interval interface{}) *GroupByHistogram {
g.Interval = interval
return g
}
func (g *GroupByHistogram) SetFieldName(fieldName string) *GroupByHistogram {
g.Field = fieldName
return g
}
func (g *GroupByHistogram) SetFiledRange(min interface{}, max interface{}) *GroupByHistogram {
g.FieldRange.Min = min
g.FieldRange.Max = max
return g
}
func (g *GroupByHistogram) SetGroupBySorters(sorters []GroupBySorter) *GroupByHistogram {
g.Sorters = sorters
return g
}