search_aggs_matrix_stats.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // MatrixMatrixStatsAggregation ...
  6. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-metrics-stats-aggregation.html
  7. // for details.
  8. type MatrixStatsAggregation struct {
  9. fields []string
  10. missing interface{}
  11. format string
  12. valueType interface{}
  13. mode string
  14. subAggregations map[string]Aggregation
  15. meta map[string]interface{}
  16. }
  17. // NewMatrixStatsAggregation initializes a new MatrixStatsAggregation.
  18. func NewMatrixStatsAggregation() *MatrixStatsAggregation {
  19. return &MatrixStatsAggregation{
  20. subAggregations: make(map[string]Aggregation),
  21. }
  22. }
  23. func (a *MatrixStatsAggregation) Fields(fields ...string) *MatrixStatsAggregation {
  24. a.fields = append(a.fields, fields...)
  25. return a
  26. }
  27. // Missing configures the value to use when documents miss a value.
  28. func (a *MatrixStatsAggregation) Missing(missing interface{}) *MatrixStatsAggregation {
  29. a.missing = missing
  30. return a
  31. }
  32. // Mode specifies how to operate. Valid values are: sum, avg, median, min, or max.
  33. func (a *MatrixStatsAggregation) Mode(mode string) *MatrixStatsAggregation {
  34. a.mode = mode
  35. return a
  36. }
  37. func (a *MatrixStatsAggregation) Format(format string) *MatrixStatsAggregation {
  38. a.format = format
  39. return a
  40. }
  41. func (a *MatrixStatsAggregation) ValueType(valueType interface{}) *MatrixStatsAggregation {
  42. a.valueType = valueType
  43. return a
  44. }
  45. func (a *MatrixStatsAggregation) SubAggregation(name string, subAggregation Aggregation) *MatrixStatsAggregation {
  46. a.subAggregations[name] = subAggregation
  47. return a
  48. }
  49. // Meta sets the meta data to be included in the aggregation response.
  50. func (a *MatrixStatsAggregation) Meta(metaData map[string]interface{}) *MatrixStatsAggregation {
  51. a.meta = metaData
  52. return a
  53. }
  54. // Source returns the JSON to serialize into the request, or an error.
  55. func (a *MatrixStatsAggregation) Source() (interface{}, error) {
  56. // Example:
  57. // {
  58. // "aggs" : {
  59. // "matrixstats" : {
  60. // "matrix_stats" : {
  61. // "fields" : ["poverty", "income"],
  62. // "missing": {"income": 50000},
  63. // "mode": "avg",
  64. // ...
  65. // }
  66. // }
  67. // }
  68. // }
  69. // This method returns only the { "matrix_stats" : { ... } } part.
  70. source := make(map[string]interface{})
  71. opts := make(map[string]interface{})
  72. source["matrix_stats"] = opts
  73. // MatrixStatsAggregationBuilder
  74. opts["fields"] = a.fields
  75. if a.missing != nil {
  76. opts["missing"] = a.missing
  77. }
  78. if a.format != "" {
  79. opts["format"] = a.format
  80. }
  81. if a.valueType != nil {
  82. opts["value_type"] = a.valueType
  83. }
  84. if a.mode != "" {
  85. opts["mode"] = a.mode
  86. }
  87. // AggregationBuilder (SubAggregations)
  88. if len(a.subAggregations) > 0 {
  89. aggsMap := make(map[string]interface{})
  90. source["aggregations"] = aggsMap
  91. for name, aggregate := range a.subAggregations {
  92. src, err := aggregate.Source()
  93. if err != nil {
  94. return nil, err
  95. }
  96. aggsMap[name] = src
  97. }
  98. }
  99. // Add Meta data if available
  100. if len(a.meta) > 0 {
  101. source["meta"] = a.meta
  102. }
  103. return source, nil
  104. }