search_aggs_avg.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2012-2015 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: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html
  11. type AvgAggregation struct {
  12. field string
  13. script string
  14. scriptFile string
  15. lang string
  16. format string
  17. params map[string]interface{}
  18. subAggregations map[string]Aggregation
  19. }
  20. func NewAvgAggregation() AvgAggregation {
  21. a := AvgAggregation{
  22. params: make(map[string]interface{}),
  23. subAggregations: make(map[string]Aggregation),
  24. }
  25. return a
  26. }
  27. func (a AvgAggregation) Field(field string) AvgAggregation {
  28. a.field = field
  29. return a
  30. }
  31. func (a AvgAggregation) Script(script string) AvgAggregation {
  32. a.script = script
  33. return a
  34. }
  35. func (a AvgAggregation) ScriptFile(scriptFile string) AvgAggregation {
  36. a.scriptFile = scriptFile
  37. return a
  38. }
  39. func (a AvgAggregation) Lang(lang string) AvgAggregation {
  40. a.lang = lang
  41. return a
  42. }
  43. func (a AvgAggregation) Format(format string) AvgAggregation {
  44. a.format = format
  45. return a
  46. }
  47. func (a AvgAggregation) Param(name string, value interface{}) AvgAggregation {
  48. a.params[name] = value
  49. return a
  50. }
  51. func (a AvgAggregation) SubAggregation(name string, subAggregation Aggregation) AvgAggregation {
  52. a.subAggregations[name] = subAggregation
  53. return a
  54. }
  55. func (a AvgAggregation) Source() interface{} {
  56. // Example:
  57. // {
  58. // "aggs" : {
  59. // "avg_grade" : { "avg" : { "field" : "grade" } }
  60. // }
  61. // }
  62. // This method returns only the { "avg" : { "field" : "grade" } } part.
  63. source := make(map[string]interface{})
  64. opts := make(map[string]interface{})
  65. source["avg"] = opts
  66. // ValuesSourceAggregationBuilder
  67. if a.field != "" {
  68. opts["field"] = a.field
  69. }
  70. if a.script != "" {
  71. opts["script"] = a.script
  72. }
  73. if a.scriptFile != "" {
  74. opts["script_file"] = a.scriptFile
  75. }
  76. if a.lang != "" {
  77. opts["lang"] = a.lang
  78. }
  79. if a.format != "" {
  80. opts["format"] = a.format
  81. }
  82. if len(a.params) > 0 {
  83. opts["params"] = a.params
  84. }
  85. // AggregationBuilder (SubAggregations)
  86. if len(a.subAggregations) > 0 {
  87. aggsMap := make(map[string]interface{})
  88. source["aggregations"] = aggsMap
  89. for name, aggregate := range a.subAggregations {
  90. aggsMap[name] = aggregate.Source()
  91. }
  92. }
  93. return source
  94. }