1 Star 1 Fork 0

宇宙蒙面侠X/github.com-olivere-elastic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
search_aggs_pipeline_bucket_sort.go 3.48 KB
一键复制 编辑 原始数据 按行查看 历史
Oliver Eilhard 提交于 2019-06-30 14:31 . Update to Elasticsearch 6.8.1
// 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.8/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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/awol2010ex/github.com-olivere-elastic.git
git@gitee.com:awol2010ex/github.com-olivere-elastic.git
awol2010ex
github.com-olivere-elastic
github.com-olivere-elastic
v6.2.36

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385