search_aggs_metrics_sum.go 2.5 KB

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