Test
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_missing.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // MissingAggregation is a field data based single bucket aggregation,
  6. // that creates a bucket of all documents in the current document set context
  7. // that are missing a field value (effectively, missing a field or having
  8. // the configured NULL value set). This aggregator will often be used in
  9. // conjunction with other field data bucket aggregators (such as ranges)
  10. // to return information for all the documents that could not be placed
  11. // in any of the other buckets due to missing field data values.
  12. // See: https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-aggregations-bucket-missing-aggregation.html
  13. type MissingAggregation struct {
  14. field string
  15. subAggregations map[string]Aggregation
  16. meta map[string]interface{}
  17. }
  18. func NewMissingAggregation() *MissingAggregation {
  19. return &MissingAggregation{
  20. subAggregations: make(map[string]Aggregation),
  21. }
  22. }
  23. func (a *MissingAggregation) Field(field string) *MissingAggregation {
  24. a.field = field
  25. return a
  26. }
  27. func (a *MissingAggregation) SubAggregation(name string, subAggregation Aggregation) *MissingAggregation {
  28. a.subAggregations[name] = subAggregation
  29. return a
  30. }
  31. // Meta sets the meta data to be included in the aggregation response.
  32. func (a *MissingAggregation) Meta(metaData map[string]interface{}) *MissingAggregation {
  33. a.meta = metaData
  34. return a
  35. }
  36. func (a *MissingAggregation) Source() (interface{}, error) {
  37. // Example:
  38. // {
  39. // "aggs" : {
  40. // "products_without_a_price" : {
  41. // "missing" : { "field" : "price" }
  42. // }
  43. // }
  44. // }
  45. // This method returns only the { "missing" : { ... } } part.
  46. source := make(map[string]interface{})
  47. opts := make(map[string]interface{})
  48. source["missing"] = opts
  49. if a.field != "" {
  50. opts["field"] = a.field
  51. }
  52. // AggregationBuilder (SubAggregations)
  53. if len(a.subAggregations) > 0 {
  54. aggsMap := make(map[string]interface{})
  55. source["aggregations"] = aggsMap
  56. for name, aggregate := range a.subAggregations {
  57. src, err := aggregate.Source()
  58. if err != nil {
  59. return nil, err
  60. }
  61. aggsMap[name] = src
  62. }
  63. }
  64. // Add Meta data if available
  65. if len(a.meta) > 0 {
  66. source["meta"] = a.meta
  67. }
  68. return source, nil
  69. }