search_aggs_metrics_value_count.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // ValueCountAggregation is a single-value metrics aggregation that counts
  6. // the number of values that are extracted from the aggregated documents.
  7. // These values can be extracted either from specific fields in the documents,
  8. // or be generated by a provided script. Typically, this aggregator will be
  9. // used in conjunction with other single-value aggregations.
  10. // For example, when computing the avg one might be interested in the
  11. // number of values the average is computed over.
  12. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-valuecount-aggregation.html
  13. type ValueCountAggregation struct {
  14. field string
  15. script *Script
  16. format string
  17. subAggregations map[string]Aggregation
  18. meta map[string]interface{}
  19. }
  20. func NewValueCountAggregation() *ValueCountAggregation {
  21. return &ValueCountAggregation{
  22. subAggregations: make(map[string]Aggregation),
  23. }
  24. }
  25. func (a *ValueCountAggregation) Field(field string) *ValueCountAggregation {
  26. a.field = field
  27. return a
  28. }
  29. func (a *ValueCountAggregation) Script(script *Script) *ValueCountAggregation {
  30. a.script = script
  31. return a
  32. }
  33. func (a *ValueCountAggregation) Format(format string) *ValueCountAggregation {
  34. a.format = format
  35. return a
  36. }
  37. func (a *ValueCountAggregation) SubAggregation(name string, subAggregation Aggregation) *ValueCountAggregation {
  38. a.subAggregations[name] = subAggregation
  39. return a
  40. }
  41. // Meta sets the meta data to be included in the aggregation response.
  42. func (a *ValueCountAggregation) Meta(metaData map[string]interface{}) *ValueCountAggregation {
  43. a.meta = metaData
  44. return a
  45. }
  46. func (a *ValueCountAggregation) Source() (interface{}, error) {
  47. // Example:
  48. // {
  49. // "aggs" : {
  50. // "grades_count" : { "value_count" : { "field" : "grade" } }
  51. // }
  52. // }
  53. // This method returns only the { "value_count" : { "field" : "grade" } } part.
  54. source := make(map[string]interface{})
  55. opts := make(map[string]interface{})
  56. source["value_count"] = opts
  57. // ValuesSourceAggregationBuilder
  58. if a.field != "" {
  59. opts["field"] = a.field
  60. }
  61. if a.script != nil {
  62. src, err := a.script.Source()
  63. if err != nil {
  64. return nil, err
  65. }
  66. opts["script"] = src
  67. }
  68. if a.format != "" {
  69. opts["format"] = a.format
  70. }
  71. // AggregationBuilder (SubAggregations)
  72. if len(a.subAggregations) > 0 {
  73. aggsMap := make(map[string]interface{})
  74. source["aggregations"] = aggsMap
  75. for name, aggregate := range a.subAggregations {
  76. src, err := aggregate.Source()
  77. if err != nil {
  78. return nil, err
  79. }
  80. aggsMap[name] = src
  81. }
  82. }
  83. // Add Meta data if available
  84. if len(a.meta) > 0 {
  85. source["meta"] = a.meta
  86. }
  87. return source, nil
  88. }