search_aggs_metrics_percentiles_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestPercentilesAggregation(t *testing.T) {
  10. agg := NewPercentilesAggregation().Field("price")
  11. src, err := agg.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"percentiles":{"field":"price"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestPercentilesAggregationWithCustomPercents(t *testing.T) {
  26. agg := NewPercentilesAggregation().Field("price").Percentiles(0.2, 0.5, 0.9)
  27. src, err := agg.Source()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. data, err := json.Marshal(src)
  32. if err != nil {
  33. t.Fatalf("marshaling to JSON failed: %v", err)
  34. }
  35. got := string(data)
  36. expected := `{"percentiles":{"field":"price","percents":[0.2,0.5,0.9]}}`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestPercentilesAggregationWithFormat(t *testing.T) {
  42. agg := NewPercentilesAggregation().Field("price").Format("00000.00")
  43. src, err := agg.Source()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. data, err := json.Marshal(src)
  48. if err != nil {
  49. t.Fatalf("marshaling to JSON failed: %v", err)
  50. }
  51. got := string(data)
  52. expected := `{"percentiles":{"field":"price","format":"00000.00"}}`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }
  57. func TestPercentilesAggregationWithMetaData(t *testing.T) {
  58. agg := NewPercentilesAggregation().Field("price").Meta(map[string]interface{}{"name": "Oliver"})
  59. src, err := agg.Source()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. data, err := json.Marshal(src)
  64. if err != nil {
  65. t.Fatalf("marshaling to JSON failed: %v", err)
  66. }
  67. got := string(data)
  68. expected := `{"meta":{"name":"Oliver"},"percentiles":{"field":"price"}}`
  69. if got != expected {
  70. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  71. }
  72. }
  73. func TestPercentilesAggregationWithCompression(t *testing.T) {
  74. agg := NewPercentilesAggregation().Field("load_time").Compression(200.0)
  75. src, err := agg.Source()
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. data, err := json.Marshal(src)
  80. if err != nil {
  81. t.Fatalf("marshaling to JSON failed: %v", err)
  82. }
  83. got := string(data)
  84. expected := `{"percentiles":{"field":"load_time","tdigest":{"compression":200}}}`
  85. if got != expected {
  86. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  87. }
  88. }
  89. func TestPercentilesAggregationWithNumberOfSignificantValueDigits(t *testing.T) {
  90. agg := NewPercentilesAggregation().
  91. Field("load_time").
  92. Percentiles(95, 99, 99.9).
  93. Method("hdr").
  94. NumberOfSignificantValueDigits(5)
  95. src, err := agg.Source()
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. data, err := json.Marshal(src)
  100. if err != nil {
  101. t.Fatalf("marshaling to JSON failed: %v", err)
  102. }
  103. got := string(data)
  104. expected := `{"percentiles":{"field":"load_time","hdr":{"number_of_significant_value_digits":5},"percents":[95,99,99.9]}}`
  105. if got != expected {
  106. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  107. }
  108. }