search_aggs_cardinality.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // 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: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html
  10. type CardinalityAggregation 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. precisionThreshold *int64
  19. rehash *bool
  20. }
  21. func NewCardinalityAggregation() CardinalityAggregation {
  22. a := CardinalityAggregation{
  23. params: make(map[string]interface{}),
  24. subAggregations: make(map[string]Aggregation),
  25. }
  26. return a
  27. }
  28. func (a CardinalityAggregation) Field(field string) CardinalityAggregation {
  29. a.field = field
  30. return a
  31. }
  32. func (a CardinalityAggregation) Script(script string) CardinalityAggregation {
  33. a.script = script
  34. return a
  35. }
  36. func (a CardinalityAggregation) ScriptFile(scriptFile string) CardinalityAggregation {
  37. a.scriptFile = scriptFile
  38. return a
  39. }
  40. func (a CardinalityAggregation) Lang(lang string) CardinalityAggregation {
  41. a.lang = lang
  42. return a
  43. }
  44. func (a CardinalityAggregation) Format(format string) CardinalityAggregation {
  45. a.format = format
  46. return a
  47. }
  48. func (a CardinalityAggregation) Param(name string, value interface{}) CardinalityAggregation {
  49. a.params[name] = value
  50. return a
  51. }
  52. func (a CardinalityAggregation) SubAggregation(name string, subAggregation Aggregation) CardinalityAggregation {
  53. a.subAggregations[name] = subAggregation
  54. return a
  55. }
  56. func (a CardinalityAggregation) PrecisionThreshold(threshold int64) CardinalityAggregation {
  57. a.precisionThreshold = &threshold
  58. return a
  59. }
  60. func (a CardinalityAggregation) Rehash(rehash bool) CardinalityAggregation {
  61. a.rehash = &rehash
  62. return a
  63. }
  64. func (a CardinalityAggregation) Source() interface{} {
  65. // Example:
  66. // {
  67. // "aggs" : {
  68. // "author_count" : {
  69. // "cardinality" : { "field" : "author" }
  70. // }
  71. // }
  72. // }
  73. // This method returns only the "cardinality" : { "field" : "author" } part.
  74. source := make(map[string]interface{})
  75. opts := make(map[string]interface{})
  76. source["cardinality"] = opts
  77. // ValuesSourceAggregationBuilder
  78. if a.field != "" {
  79. opts["field"] = a.field
  80. }
  81. if a.script != "" {
  82. opts["script"] = a.script
  83. }
  84. if a.scriptFile != "" {
  85. opts["script_file"] = a.scriptFile
  86. }
  87. if a.lang != "" {
  88. opts["lang"] = a.lang
  89. }
  90. if a.format != "" {
  91. opts["format"] = a.format
  92. }
  93. if len(a.params) > 0 {
  94. opts["params"] = a.params
  95. }
  96. if a.precisionThreshold != nil {
  97. opts["precision_threshold"] = *a.precisionThreshold
  98. }
  99. if a.rehash != nil {
  100. opts["rehash"] = *a.rehash
  101. }
  102. // AggregationBuilder (SubAggregations)
  103. if len(a.subAggregations) > 0 {
  104. aggsMap := make(map[string]interface{})
  105. source["aggregations"] = aggsMap
  106. for name, aggregate := range a.subAggregations {
  107. aggsMap[name] = aggregate.Source()
  108. }
  109. }
  110. return source
  111. }