소스 검색

Add `percents` to Percentiles Bucket aggregation

Oliver Eilhard 2 년 전
부모
커밋
fafb2b934f
2개의 변경된 파일33개의 추가작업 그리고 4개의 파일을 삭제
  1. 16
    4
      search_aggs_pipeline_percentiles_bucket.go
  2. 17
    0
      search_aggs_pipeline_percentiles_bucket_test.go

+ 16
- 4
search_aggs_pipeline_percentiles_bucket.go 파일 보기

@@ -12,27 +12,34 @@ package elastic
12 12
 // For more details, see
13 13
 // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-percentiles-bucket-aggregation.html
14 14
 type PercentilesBucketAggregation struct {
15
-	format    string
16
-	gapPolicy string
15
+	format       string
16
+	gapPolicy    string
17
+	percents     []float64
18
+	bucketsPaths []string
17 19
 
18 20
 	subAggregations map[string]Aggregation
19 21
 	meta            map[string]interface{}
20
-	bucketsPaths    []string
21 22
 }
22 23
 
23 24
 // NewPercentilesBucketAggregation creates and initializes a new PercentilesBucketAggregation.
24 25
 func NewPercentilesBucketAggregation() *PercentilesBucketAggregation {
25 26
 	return &PercentilesBucketAggregation{
26 27
 		subAggregations: make(map[string]Aggregation),
27
-		bucketsPaths:    make([]string, 0),
28 28
 	}
29 29
 }
30 30
 
31
+// Format to apply the output value of this aggregation.
31 32
 func (p *PercentilesBucketAggregation) Format(format string) *PercentilesBucketAggregation {
32 33
 	p.format = format
33 34
 	return p
34 35
 }
35 36
 
37
+// Percents to calculate percentiles for in this aggregation.
38
+func (p *PercentilesBucketAggregation) Percents(percents ...float64) *PercentilesBucketAggregation {
39
+	p.percents = percents
40
+	return p
41
+}
42
+
36 43
 // GapPolicy defines what should be done when a gap in the series is discovered.
37 44
 // Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
38 45
 func (p *PercentilesBucketAggregation) GapPolicy(gapPolicy string) *PercentilesBucketAggregation {
@@ -91,6 +98,11 @@ func (p *PercentilesBucketAggregation) Source() (interface{}, error) {
91 98
 		params["buckets_path"] = p.bucketsPaths
92 99
 	}
93 100
 
101
+	// Add percents
102
+	if len(p.percents) > 0 {
103
+		params["percents"] = p.percents
104
+	}
105
+
94 106
 	// AggregationBuilder (SubAggregations)
95 107
 	if len(p.subAggregations) > 0 {
96 108
 		aggsMap := make(map[string]interface{})

+ 17
- 0
search_aggs_pipeline_percentiles_bucket_test.go 파일 보기

@@ -25,3 +25,20 @@ func TestPercentilesBucketAggregation(t *testing.T) {
25 25
 		t.Errorf("expected\n%s\n,got:\n%s", expected, got)
26 26
 	}
27 27
 }
28
+
29
+func TestPercentilesBucketAggregationWithPercents(t *testing.T) {
30
+	agg := NewPercentilesBucketAggregation().BucketsPath("the_sum").Percents(0.1, 1.0, 5.0, 25, 50)
31
+	src, err := agg.Source()
32
+	if err != nil {
33
+		t.Fatal(err)
34
+	}
35
+	data, err := json.Marshal(src)
36
+	if err != nil {
37
+		t.Fatalf("marshaling to JSON failed: %v", err)
38
+	}
39
+	got := string(data)
40
+	expected := `{"percentiles_bucket":{"buckets_path":"the_sum","percents":[0.1,1,5,25,50]}}`
41
+	if got != expected {
42
+		t.Errorf("expected\n%s\n,got:\n%s", expected, got)
43
+	}
44
+}

Loading…
취소
저장