search_aggs_pipeline_mov_avg_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 TestMovAvgAggregation(t *testing.T) {
  10. agg := NewMovAvgAggregation().BucketsPath("the_sum")
  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 := `{"moving_avg":{"buckets_path":"the_sum"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestMovAvgAggregationWithSimpleModel(t *testing.T) {
  26. agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).Model(NewSimpleMovAvgModel())
  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 := `{"moving_avg":{"buckets_path":"the_sum","model":"simple","window":30}}`
  37. if got != expected {
  38. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  39. }
  40. }
  41. func TestMovAvgAggregationWithLinearModel(t *testing.T) {
  42. agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).Model(NewLinearMovAvgModel())
  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 := `{"moving_avg":{"buckets_path":"the_sum","model":"linear","window":30}}`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }
  57. func TestMovAvgAggregationWithEWMAModel(t *testing.T) {
  58. agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).Model(NewEWMAMovAvgModel().Alpha(0.5))
  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 := `{"moving_avg":{"buckets_path":"the_sum","model":"ewma","settings":{"alpha":0.5},"window":30}}`
  69. if got != expected {
  70. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  71. }
  72. }
  73. func TestMovAvgAggregationWithHoltLinearModel(t *testing.T) {
  74. agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).
  75. Model(NewHoltLinearMovAvgModel().Alpha(0.5).Beta(0.4))
  76. src, err := agg.Source()
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. data, err := json.Marshal(src)
  81. if err != nil {
  82. t.Fatalf("marshaling to JSON failed: %v", err)
  83. }
  84. got := string(data)
  85. expected := `{"moving_avg":{"buckets_path":"the_sum","model":"holt","settings":{"alpha":0.5,"beta":0.4},"window":30}}`
  86. if got != expected {
  87. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  88. }
  89. }
  90. func TestMovAvgAggregationWithHoltWintersModel(t *testing.T) {
  91. agg := NewMovAvgAggregation().BucketsPath("the_sum").Window(30).Predict(10).Minimize(true).
  92. Model(NewHoltWintersMovAvgModel().Alpha(0.5).Beta(0.4).Gamma(0.3).Period(7).Pad(true))
  93. src, err := agg.Source()
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. data, err := json.Marshal(src)
  98. if err != nil {
  99. t.Fatalf("marshaling to JSON failed: %v", err)
  100. }
  101. got := string(data)
  102. expected := `{"moving_avg":{"buckets_path":"the_sum","minimize":true,"model":"holt_winters","predict":10,"settings":{"alpha":0.5,"beta":0.4,"gamma":0.3,"pad":true,"period":7},"window":30}}`
  103. if got != expected {
  104. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  105. }
  106. }
  107. func TestMovAvgAggregationWithSubAggs(t *testing.T) {
  108. agg := NewMovAvgAggregation().BucketsPath("the_sum")
  109. agg = agg.SubAggregation("avg_sum", NewAvgAggregation().Field("height"))
  110. src, err := agg.Source()
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. data, err := json.Marshal(src)
  115. if err != nil {
  116. t.Fatalf("marshaling to JSON failed: %v", err)
  117. }
  118. got := string(data)
  119. expected := `{"aggregations":{"avg_sum":{"avg":{"field":"height"}}},"moving_avg":{"buckets_path":"the_sum"}}`
  120. if got != expected {
  121. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  122. }
  123. }