search_aggs_metrics_cardinality.go 3.1 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. // CardinalityAggregation is a single-value metrics aggregation that
  6. // calculates an approximate count of distinct values.
  7. // Values can be extracted either from specific fields in the document
  8. // or generated by a script.
  9. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-cardinality-aggregation.html
  10. type CardinalityAggregation struct {
  11. field string
  12. script *Script
  13. format string
  14. subAggregations map[string]Aggregation
  15. meta map[string]interface{}
  16. precisionThreshold *int64
  17. rehash *bool
  18. }
  19. func NewCardinalityAggregation() *CardinalityAggregation {
  20. return &CardinalityAggregation{
  21. subAggregations: make(map[string]Aggregation),
  22. }
  23. }
  24. func (a *CardinalityAggregation) Field(field string) *CardinalityAggregation {
  25. a.field = field
  26. return a
  27. }
  28. func (a *CardinalityAggregation) Script(script *Script) *CardinalityAggregation {
  29. a.script = script
  30. return a
  31. }
  32. func (a *CardinalityAggregation) Format(format string) *CardinalityAggregation {
  33. a.format = format
  34. return a
  35. }
  36. func (a *CardinalityAggregation) SubAggregation(name string, subAggregation Aggregation) *CardinalityAggregation {
  37. a.subAggregations[name] = subAggregation
  38. return a
  39. }
  40. // Meta sets the meta data to be included in the aggregation response.
  41. func (a *CardinalityAggregation) Meta(metaData map[string]interface{}) *CardinalityAggregation {
  42. a.meta = metaData
  43. return a
  44. }
  45. func (a *CardinalityAggregation) PrecisionThreshold(threshold int64) *CardinalityAggregation {
  46. a.precisionThreshold = &threshold
  47. return a
  48. }
  49. func (a *CardinalityAggregation) Rehash(rehash bool) *CardinalityAggregation {
  50. a.rehash = &rehash
  51. return a
  52. }
  53. func (a *CardinalityAggregation) Source() (interface{}, error) {
  54. // Example:
  55. // {
  56. // "aggs" : {
  57. // "author_count" : {
  58. // "cardinality" : { "field" : "author" }
  59. // }
  60. // }
  61. // }
  62. // This method returns only the "cardinality" : { "field" : "author" } part.
  63. source := make(map[string]interface{})
  64. opts := make(map[string]interface{})
  65. source["cardinality"] = opts
  66. // ValuesSourceAggregationBuilder
  67. if a.field != "" {
  68. opts["field"] = a.field
  69. }
  70. if a.script != nil {
  71. src, err := a.script.Source()
  72. if err != nil {
  73. return nil, err
  74. }
  75. opts["script"] = src
  76. }
  77. if a.format != "" {
  78. opts["format"] = a.format
  79. }
  80. if a.precisionThreshold != nil {
  81. opts["precision_threshold"] = *a.precisionThreshold
  82. }
  83. if a.rehash != nil {
  84. opts["rehash"] = *a.rehash
  85. }
  86. // AggregationBuilder (SubAggregations)
  87. if len(a.subAggregations) > 0 {
  88. aggsMap := make(map[string]interface{})
  89. source["aggregations"] = aggsMap
  90. for name, aggregate := range a.subAggregations {
  91. src, err := aggregate.Source()
  92. if err != nil {
  93. return nil, err
  94. }
  95. aggsMap[name] = src
  96. }
  97. }
  98. // Add Meta data if available
  99. if len(a.meta) > 0 {
  100. source["meta"] = a.meta
  101. }
  102. return source, nil
  103. }