You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_aggs_bucket_filters.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. import "errors"
  6. // FiltersAggregation defines a multi bucket aggregations where each bucket
  7. // is associated with a filter. Each bucket will collect all documents that
  8. // match its associated filter.
  9. //
  10. // Notice that the caller has to decide whether to add filters by name
  11. // (using FilterWithName) or unnamed filters (using Filter or Filters). One cannot
  12. // use both named and unnamed filters.
  13. //
  14. // For details, see
  15. // https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-filters-aggregation.html
  16. type FiltersAggregation struct {
  17. unnamedFilters []Query
  18. namedFilters map[string]Query
  19. subAggregations map[string]Aggregation
  20. meta map[string]interface{}
  21. }
  22. // NewFiltersAggregation initializes a new FiltersAggregation.
  23. func NewFiltersAggregation() *FiltersAggregation {
  24. return &FiltersAggregation{
  25. unnamedFilters: make([]Query, 0),
  26. namedFilters: make(map[string]Query),
  27. subAggregations: make(map[string]Aggregation),
  28. }
  29. }
  30. // Filter adds an unnamed filter. Notice that you can
  31. // either use named or unnamed filters, but not both.
  32. func (a *FiltersAggregation) Filter(filter Query) *FiltersAggregation {
  33. a.unnamedFilters = append(a.unnamedFilters, filter)
  34. return a
  35. }
  36. // Filters adds one or more unnamed filters. Notice that you can
  37. // either use named or unnamed filters, but not both.
  38. func (a *FiltersAggregation) Filters(filters ...Query) *FiltersAggregation {
  39. if len(filters) > 0 {
  40. a.unnamedFilters = append(a.unnamedFilters, filters...)
  41. }
  42. return a
  43. }
  44. // FilterWithName adds a filter with a specific name. Notice that you can
  45. // either use named or unnamed filters, but not both.
  46. func (a *FiltersAggregation) FilterWithName(name string, filter Query) *FiltersAggregation {
  47. a.namedFilters[name] = filter
  48. return a
  49. }
  50. // SubAggregation adds a sub-aggregation to this aggregation.
  51. func (a *FiltersAggregation) SubAggregation(name string, subAggregation Aggregation) *FiltersAggregation {
  52. a.subAggregations[name] = subAggregation
  53. return a
  54. }
  55. // Meta sets the meta data to be included in the aggregation response.
  56. func (a *FiltersAggregation) Meta(metaData map[string]interface{}) *FiltersAggregation {
  57. a.meta = metaData
  58. return a
  59. }
  60. // Source returns the a JSON-serializable interface.
  61. // If the aggregation is invalid, an error is returned. This may e.g. happen
  62. // if you mixed named and unnamed filters.
  63. func (a *FiltersAggregation) Source() (interface{}, error) {
  64. // Example:
  65. // {
  66. // "aggs" : {
  67. // "messages" : {
  68. // "filters" : {
  69. // "filters" : {
  70. // "errors" : { "term" : { "body" : "error" }},
  71. // "warnings" : { "term" : { "body" : "warning" }}
  72. // }
  73. // }
  74. // }
  75. // }
  76. // }
  77. // This method returns only the (outer) { "filters" : {} } part.
  78. source := make(map[string]interface{})
  79. filters := make(map[string]interface{})
  80. source["filters"] = filters
  81. if len(a.unnamedFilters) > 0 && len(a.namedFilters) > 0 {
  82. return nil, errors.New("elastic: use either named or unnamed filters with FiltersAggregation but not both")
  83. }
  84. if len(a.unnamedFilters) > 0 {
  85. arr := make([]interface{}, len(a.unnamedFilters))
  86. for i, filter := range a.unnamedFilters {
  87. src, err := filter.Source()
  88. if err != nil {
  89. return nil, err
  90. }
  91. arr[i] = src
  92. }
  93. filters["filters"] = arr
  94. } else {
  95. dict := make(map[string]interface{})
  96. for key, filter := range a.namedFilters {
  97. src, err := filter.Source()
  98. if err != nil {
  99. return nil, err
  100. }
  101. dict[key] = src
  102. }
  103. filters["filters"] = dict
  104. }
  105. // AggregationBuilder (SubAggregations)
  106. if len(a.subAggregations) > 0 {
  107. aggsMap := make(map[string]interface{})
  108. source["aggregations"] = aggsMap
  109. for name, aggregate := range a.subAggregations {
  110. src, err := aggregate.Source()
  111. if err != nil {
  112. return nil, err
  113. }
  114. aggsMap[name] = src
  115. }
  116. }
  117. // Add Meta data if available
  118. if len(a.meta) > 0 {
  119. source["meta"] = a.meta
  120. }
  121. return source, nil
  122. }