search_aggs_stats.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2012-2015 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: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html
  10. type StatsAggregation struct {
  11. field string
  12. script string
  13. scriptFile string
  14. lang string
  15. format string
  16. params map[string]interface{}
  17. subAggregations map[string]Aggregation
  18. }
  19. func NewStatsAggregation() StatsAggregation {
  20. a := StatsAggregation{
  21. params: make(map[string]interface{}),
  22. subAggregations: make(map[string]Aggregation),
  23. }
  24. return a
  25. }
  26. func (a StatsAggregation) Field(field string) StatsAggregation {
  27. a.field = field
  28. return a
  29. }
  30. func (a StatsAggregation) Script(script string) StatsAggregation {
  31. a.script = script
  32. return a
  33. }
  34. func (a StatsAggregation) ScriptFile(scriptFile string) StatsAggregation {
  35. a.scriptFile = scriptFile
  36. return a
  37. }
  38. func (a StatsAggregation) Lang(lang string) StatsAggregation {
  39. a.lang = lang
  40. return a
  41. }
  42. func (a StatsAggregation) Format(format string) StatsAggregation {
  43. a.format = format
  44. return a
  45. }
  46. func (a StatsAggregation) Param(name string, value interface{}) StatsAggregation {
  47. a.params[name] = value
  48. return a
  49. }
  50. func (a StatsAggregation) SubAggregation(name string, subAggregation Aggregation) StatsAggregation {
  51. a.subAggregations[name] = subAggregation
  52. return a
  53. }
  54. func (a StatsAggregation) Source() interface{} {
  55. // Example:
  56. // {
  57. // "aggs" : {
  58. // "grades_stats" : { "stats" : { "field" : "grade" } }
  59. // }
  60. // }
  61. // This method returns only the { "stats" : { "field" : "grade" } } part.
  62. source := make(map[string]interface{})
  63. opts := make(map[string]interface{})
  64. source["stats"] = opts
  65. // ValuesSourceAggregationBuilder
  66. if a.field != "" {
  67. opts["field"] = a.field
  68. }
  69. if a.script != "" {
  70. opts["script"] = a.script
  71. }
  72. if a.scriptFile != "" {
  73. opts["script_file"] = a.scriptFile
  74. }
  75. if a.lang != "" {
  76. opts["lang"] = a.lang
  77. }
  78. if a.format != "" {
  79. opts["format"] = a.format
  80. }
  81. if len(a.params) > 0 {
  82. opts["params"] = a.params
  83. }
  84. // AggregationBuilder (SubAggregations)
  85. if len(a.subAggregations) > 0 {
  86. aggsMap := make(map[string]interface{})
  87. source["aggregations"] = aggsMap
  88. for name, aggregate := range a.subAggregations {
  89. aggsMap[name] = aggregate.Source()
  90. }
  91. }
  92. return source
  93. }