search_aggs_value_count.go 3.0 KB

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