insight support terms top by sorted sub metric

This commit is contained in:
liugq 2023-07-03 16:09:22 +08:00
parent 7336ebc208
commit cc25b07439
2 changed files with 18 additions and 4 deletions

View File

@ -16,6 +16,7 @@ type Metric struct {
BucketSize string `json:"bucket_size,omitempty"`
Filter interface{} `json:"filter,omitempty"`
Groups []MetricGroupItem `json:"groups,omitempty"` //bucket group
Sort []GroupSort `json:"sort,omitempty"`
ClusterId string `json:"cluster_id,omitempty"`
Formula string `json:"formula,omitempty"`
Items []MetricItem `json:"items"`
@ -25,6 +26,11 @@ type Metric struct {
BucketLabel *BucketLabel `json:"bucket_label,omitempty"`
}
type GroupSort struct {
Key string `json:"key"`
Direction string `json:"direction"`
}
type MetricGroupItem struct {
Field string `json:"field"`
Limit int `json:"limit"`

View File

@ -125,11 +125,19 @@ func GenerateQuery(metric *insight.Metric) (interface{}, error) {
if limit <= 0 {
limit = 10
}
termsCfg := util.MapStr{
"field": groups[i].Field,
"size": limit,
}
if i == grpLength - 1 && len(metric.Sort) > 0 {
var termsOrder []interface{}
for _, sortItem := range metric.Sort {
termsOrder = append(termsOrder, util.MapStr{sortItem.Key: sortItem.Direction})
}
termsCfg["order"] = termsOrder
}
groupAgg := util.MapStr{
"terms": util.MapStr{
"field": groups[i].Field,
"size": limit,
},
"terms": termsCfg,
}
groupID := util.GetUUID()
if lastGroupAgg != nil {