選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

search_aggs_metrics_cardinality.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/7.0/search-aggregations-metrics-cardinality-aggregation.html
  10. type CardinalityAggregation struct {
  11. field string
  12. script *Script
  13. format string
  14. missing interface{}
  15. subAggregations map[string]Aggregation
  16. meta map[string]interface{}
  17. precisionThreshold *int64
  18. rehash *bool
  19. }
  20. func NewCardinalityAggregation() *CardinalityAggregation {
  21. return &CardinalityAggregation{
  22. subAggregations: make(map[string]Aggregation),
  23. }
  24. }
  25. func (a *CardinalityAggregation) Field(field string) *CardinalityAggregation {
  26. a.field = field
  27. return a
  28. }
  29. func (a *CardinalityAggregation) Script(script *Script) *CardinalityAggregation {
  30. a.script = script
  31. return a
  32. }
  33. func (a *CardinalityAggregation) Format(format string) *CardinalityAggregation {
  34. a.format = format
  35. return a
  36. }
  37. func (a *CardinalityAggregation) Missing(missing interface{}) *CardinalityAggregation {
  38. a.missing = missing
  39. return a
  40. }
  41. func (a *CardinalityAggregation) SubAggregation(name string, subAggregation Aggregation) *CardinalityAggregation {
  42. a.subAggregations[name] = subAggregation
  43. return a
  44. }
  45. // Meta sets the meta data to be included in the aggregation response.
  46. func (a *CardinalityAggregation) Meta(metaData map[string]interface{}) *CardinalityAggregation {
  47. a.meta = metaData
  48. return a
  49. }
  50. func (a *CardinalityAggregation) PrecisionThreshold(threshold int64) *CardinalityAggregation {
  51. a.precisionThreshold = &threshold
  52. return a
  53. }
  54. func (a *CardinalityAggregation) Rehash(rehash bool) *CardinalityAggregation {
  55. a.rehash = &rehash
  56. return a
  57. }
  58. func (a *CardinalityAggregation) Source() (interface{}, error) {
  59. // Example:
  60. // {
  61. // "aggs" : {
  62. // "author_count" : {
  63. // "cardinality" : { "field" : "author" }
  64. // }
  65. // }
  66. // }
  67. // This method returns only the "cardinality" : { "field" : "author" } part.
  68. source := make(map[string]interface{})
  69. opts := make(map[string]interface{})
  70. source["cardinality"] = opts
  71. // ValuesSourceAggregationBuilder
  72. if a.field != "" {
  73. opts["field"] = a.field
  74. }
  75. if a.script != nil {
  76. src, err := a.script.Source()
  77. if err != nil {
  78. return nil, err
  79. }
  80. opts["script"] = src
  81. }
  82. if a.missing != nil {
  83. opts["missing"] = a.missing
  84. }
  85. if a.format != "" {
  86. opts["format"] = a.format
  87. }
  88. if a.precisionThreshold != nil {
  89. opts["precision_threshold"] = *a.precisionThreshold
  90. }
  91. if a.rehash != nil {
  92. opts["rehash"] = *a.rehash
  93. }
  94. // AggregationBuilder (SubAggregations)
  95. if len(a.subAggregations) > 0 {
  96. aggsMap := make(map[string]interface{})
  97. source["aggregations"] = aggsMap
  98. for name, aggregate := range a.subAggregations {
  99. src, err := aggregate.Source()
  100. if err != nil {
  101. return nil, err
  102. }
  103. aggsMap[name] = src
  104. }
  105. }
  106. // Add Meta data if available
  107. if len(a.meta) > 0 {
  108. source["meta"] = a.meta
  109. }
  110. return source, nil
  111. }