Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

search_aggs_metrics_avg.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // AvgAggregation is a single-value metrics aggregation that computes
  6. // the average of numeric values that are extracted from the
  7. // aggregated documents. These values can be extracted either from
  8. // specific numeric fields in the documents, or be generated by
  9. // a provided script.
  10. //
  11. // See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-avg-aggregation.html
  12. type AvgAggregation struct {
  13. field string
  14. script *Script
  15. format string
  16. missing interface{}
  17. subAggregations map[string]Aggregation
  18. meta map[string]interface{}
  19. }
  20. func NewAvgAggregation() *AvgAggregation {
  21. return &AvgAggregation{
  22. subAggregations: make(map[string]Aggregation),
  23. }
  24. }
  25. func (a *AvgAggregation) Field(field string) *AvgAggregation {
  26. a.field = field
  27. return a
  28. }
  29. func (a *AvgAggregation) Script(script *Script) *AvgAggregation {
  30. a.script = script
  31. return a
  32. }
  33. func (a *AvgAggregation) Format(format string) *AvgAggregation {
  34. a.format = format
  35. return a
  36. }
  37. func (a *AvgAggregation) Missing(missing interface{}) *AvgAggregation {
  38. a.missing = missing
  39. return a
  40. }
  41. func (a *AvgAggregation) SubAggregation(name string, subAggregation Aggregation) *AvgAggregation {
  42. a.subAggregations[name] = subAggregation
  43. return a
  44. }
  45. // Meta sets the meta data to be included in the aggregation response.
  46. func (a *AvgAggregation) Meta(metaData map[string]interface{}) *AvgAggregation {
  47. a.meta = metaData
  48. return a
  49. }
  50. func (a *AvgAggregation) Source() (interface{}, error) {
  51. // Example:
  52. // {
  53. // "aggs" : {
  54. // "avg_grade" : { "avg" : { "field" : "grade" } }
  55. // }
  56. // }
  57. // This method returns only the { "avg" : { "field" : "grade" } } part.
  58. source := make(map[string]interface{})
  59. opts := make(map[string]interface{})
  60. source["avg"] = opts
  61. // ValuesSourceAggregationBuilder
  62. if a.field != "" {
  63. opts["field"] = a.field
  64. }
  65. if a.script != nil {
  66. src, err := a.script.Source()
  67. if err != nil {
  68. return nil, err
  69. }
  70. opts["script"] = src
  71. }
  72. if a.format != "" {
  73. opts["format"] = a.format
  74. }
  75. if a.missing != nil {
  76. opts["missing"] = a.missing
  77. }
  78. // AggregationBuilder (SubAggregations)
  79. if len(a.subAggregations) > 0 {
  80. aggsMap := make(map[string]interface{})
  81. source["aggregations"] = aggsMap
  82. for name, aggregate := range a.subAggregations {
  83. src, err := aggregate.Source()
  84. if err != nil {
  85. return nil, err
  86. }
  87. aggsMap[name] = src
  88. }
  89. }
  90. // Add Meta data if available
  91. if len(a.meta) > 0 {
  92. source["meta"] = a.meta
  93. }
  94. return source, nil
  95. }