search_aggs_metrics_stats.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. // StatsAggregation is a multi-value metrics aggregation that computes stats
  6. // over numeric values 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-stats-aggregation.html
  10. type StatsAggregation struct {
  11. field string
  12. script *Script
  13. format string
  14. subAggregations map[string]Aggregation
  15. meta map[string]interface{}
  16. }
  17. func NewStatsAggregation() *StatsAggregation {
  18. return &StatsAggregation{
  19. subAggregations: make(map[string]Aggregation),
  20. }
  21. }
  22. func (a *StatsAggregation) Field(field string) *StatsAggregation {
  23. a.field = field
  24. return a
  25. }
  26. func (a *StatsAggregation) Script(script *Script) *StatsAggregation {
  27. a.script = script
  28. return a
  29. }
  30. func (a *StatsAggregation) Format(format string) *StatsAggregation {
  31. a.format = format
  32. return a
  33. }
  34. func (a *StatsAggregation) SubAggregation(name string, subAggregation Aggregation) *StatsAggregation {
  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 *StatsAggregation) Meta(metaData map[string]interface{}) *StatsAggregation {
  40. a.meta = metaData
  41. return a
  42. }
  43. func (a *StatsAggregation) Source() (interface{}, error) {
  44. // Example:
  45. // {
  46. // "aggs" : {
  47. // "grades_stats" : { "stats" : { "field" : "grade" } }
  48. // }
  49. // }
  50. // This method returns only the { "stats" : { "field" : "grade" } } part.
  51. source := make(map[string]interface{})
  52. opts := make(map[string]interface{})
  53. source["stats"] = 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. }