Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

search_aggs_metrics_weighted_avg.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // WeightedAvgAggregation is a single-value metrics aggregation that
  6. // computes the weighted average of numeric values that are extracted
  7. // from the aggregated documents. These values can be extracted either
  8. // from specific numeric fields in the documents.
  9. //
  10. // See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-weight-avg-aggregation.html
  11. type WeightedAvgAggregation struct {
  12. fields map[string]*MultiValuesSourceFieldConfig
  13. valueType string
  14. format string
  15. value *MultiValuesSourceFieldConfig
  16. weight *MultiValuesSourceFieldConfig
  17. subAggregations map[string]Aggregation
  18. meta map[string]interface{}
  19. }
  20. func NewWeightedAvgAggregation() *WeightedAvgAggregation {
  21. return &WeightedAvgAggregation{
  22. fields: make(map[string]*MultiValuesSourceFieldConfig),
  23. subAggregations: make(map[string]Aggregation),
  24. }
  25. }
  26. func (a *WeightedAvgAggregation) Field(field string, config *MultiValuesSourceFieldConfig) *WeightedAvgAggregation {
  27. a.fields[field] = config
  28. return a
  29. }
  30. func (a *WeightedAvgAggregation) ValueType(valueType string) *WeightedAvgAggregation {
  31. a.valueType = valueType
  32. return a
  33. }
  34. func (a *WeightedAvgAggregation) Format(format string) *WeightedAvgAggregation {
  35. a.format = format
  36. return a
  37. }
  38. func (a *WeightedAvgAggregation) Value(value *MultiValuesSourceFieldConfig) *WeightedAvgAggregation {
  39. a.value = value
  40. return a
  41. }
  42. func (a *WeightedAvgAggregation) Weight(weight *MultiValuesSourceFieldConfig) *WeightedAvgAggregation {
  43. a.weight = weight
  44. return a
  45. }
  46. func (a *WeightedAvgAggregation) SubAggregation(name string, subAggregation Aggregation) *WeightedAvgAggregation {
  47. a.subAggregations[name] = subAggregation
  48. return a
  49. }
  50. // Meta sets the meta data to be included in the aggregation response.
  51. func (a *WeightedAvgAggregation) Meta(metaData map[string]interface{}) *WeightedAvgAggregation {
  52. a.meta = metaData
  53. return a
  54. }
  55. func (a *WeightedAvgAggregation) Source() (interface{}, error) {
  56. source := make(map[string]interface{})
  57. opts := make(map[string]interface{})
  58. source["weighted_avg"] = opts
  59. if len(a.fields) > 0 {
  60. f := make(map[string]interface{})
  61. for name, config := range a.fields {
  62. cfg, err := config.Source()
  63. if err != nil {
  64. return nil, err
  65. }
  66. f[name] = cfg
  67. }
  68. opts["fields"] = f
  69. }
  70. if v := a.format; v != "" {
  71. opts["format"] = v
  72. }
  73. if v := a.valueType; v != "" {
  74. opts["value_type"] = v
  75. }
  76. if v := a.value; v != nil {
  77. cfg, err := v.Source()
  78. if err != nil {
  79. return nil, err
  80. }
  81. opts["value"] = cfg
  82. }
  83. if v := a.weight; v != nil {
  84. cfg, err := v.Source()
  85. if err != nil {
  86. return nil, err
  87. }
  88. opts["weight"] = cfg
  89. }
  90. // AggregationBuilder (SubAggregations)
  91. if len(a.subAggregations) > 0 {
  92. aggsMap := make(map[string]interface{})
  93. source["aggregations"] = aggsMap
  94. for name, aggregate := range a.subAggregations {
  95. src, err := aggregate.Source()
  96. if err != nil {
  97. return nil, err
  98. }
  99. aggsMap[name] = src
  100. }
  101. }
  102. // Add Meta data if available
  103. if len(a.meta) > 0 {
  104. source["meta"] = a.meta
  105. }
  106. return source, nil
  107. }
  108. // MultiValuesSourceFieldConfig represents a field configuration
  109. // used e.g. in WeightedAvgAggregation.
  110. type MultiValuesSourceFieldConfig struct {
  111. FieldName string
  112. Missing interface{}
  113. Script *Script
  114. TimeZone string
  115. }
  116. func (f *MultiValuesSourceFieldConfig) Source() (interface{}, error) {
  117. source := make(map[string]interface{})
  118. if v := f.Missing; v != nil {
  119. source["missing"] = v
  120. }
  121. if v := f.Script; v != nil {
  122. src, err := v.Source()
  123. if err != nil {
  124. return nil, err
  125. }
  126. source["script"] = src
  127. }
  128. if v := f.FieldName; v != "" {
  129. source["field"] = v
  130. }
  131. if v := f.TimeZone; v != "" {
  132. source["time_zone"] = v
  133. }
  134. return source, nil
  135. }