search_aggs_metrics_avg.go 2.5 KB

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