search_aggs_metrics_max.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. // MaxAggregation is a single-value metrics aggregation that keeps track and
  6. // returns the maximum value among the numeric values extracted from
  7. // the 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-max-aggregation.html
  11. type MaxAggregation struct {
  12. field string
  13. script *Script
  14. format string
  15. subAggregations map[string]Aggregation
  16. meta map[string]interface{}
  17. }
  18. func NewMaxAggregation() *MaxAggregation {
  19. return &MaxAggregation{
  20. subAggregations: make(map[string]Aggregation),
  21. }
  22. }
  23. func (a *MaxAggregation) Field(field string) *MaxAggregation {
  24. a.field = field
  25. return a
  26. }
  27. func (a *MaxAggregation) Script(script *Script) *MaxAggregation {
  28. a.script = script
  29. return a
  30. }
  31. func (a *MaxAggregation) Format(format string) *MaxAggregation {
  32. a.format = format
  33. return a
  34. }
  35. func (a *MaxAggregation) SubAggregation(name string, subAggregation Aggregation) *MaxAggregation {
  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 *MaxAggregation) Meta(metaData map[string]interface{}) *MaxAggregation {
  41. a.meta = metaData
  42. return a
  43. }
  44. func (a *MaxAggregation) Source() (interface{}, error) {
  45. // Example:
  46. // {
  47. // "aggs" : {
  48. // "max_price" : { "max" : { "field" : "price" } }
  49. // }
  50. // }
  51. // This method returns only the { "max" : { "field" : "price" } } part.
  52. source := make(map[string]interface{})
  53. opts := make(map[string]interface{})
  54. source["max"] = 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. }