Parcourir la source

Add `percents` to Percentiles Bucket aggregation

tags/v5.0.39
Oliver Eilhard il y a 2 ans
Parent
révision
fafb2b934f

+ 16
- 4
search_aggs_pipeline_percentiles_bucket.go Voir le fichier

@@ -12,27 +12,34 @@ package elastic
// For more details, see
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-percentiles-bucket-aggregation.html
type PercentilesBucketAggregation struct {
format string
gapPolicy string
format string
gapPolicy string
percents []float64
bucketsPaths []string

subAggregations map[string]Aggregation
meta map[string]interface{}
bucketsPaths []string
}

// NewPercentilesBucketAggregation creates and initializes a new PercentilesBucketAggregation.
func NewPercentilesBucketAggregation() *PercentilesBucketAggregation {
return &PercentilesBucketAggregation{
subAggregations: make(map[string]Aggregation),
bucketsPaths: make([]string, 0),
}
}

// Format to apply the output value of this aggregation.
func (p *PercentilesBucketAggregation) Format(format string) *PercentilesBucketAggregation {
p.format = format
return p
}

// Percents to calculate percentiles for in this aggregation.
func (p *PercentilesBucketAggregation) Percents(percents ...float64) *PercentilesBucketAggregation {
p.percents = percents
return p
}

// GapPolicy defines what should be done when a gap in the series is discovered.
// Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
func (p *PercentilesBucketAggregation) GapPolicy(gapPolicy string) *PercentilesBucketAggregation {
@@ -91,6 +98,11 @@ func (p *PercentilesBucketAggregation) Source() (interface{}, error) {
params["buckets_path"] = p.bucketsPaths
}

// Add percents
if len(p.percents) > 0 {
params["percents"] = p.percents
}

// AggregationBuilder (SubAggregations)
if len(p.subAggregations) > 0 {
aggsMap := make(map[string]interface{})

+ 17
- 0
search_aggs_pipeline_percentiles_bucket_test.go Voir le fichier

@@ -25,3 +25,20 @@ func TestPercentilesBucketAggregation(t *testing.T) {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestPercentilesBucketAggregationWithPercents(t *testing.T) {
agg := NewPercentilesBucketAggregation().BucketsPath("the_sum").Percents(0.1, 1.0, 5.0, 25, 50)
src, err := agg.Source()
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"percentiles_bucket":{"buckets_path":"the_sum","percents":[0.1,1,5,25,50]}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

Chargement…
Annuler
Enregistrer