search_aggs_missing.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // 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: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-missing-aggregation.html
  13. type MissingAggregation struct {
  14. field string
  15. subAggregations map[string]Aggregation
  16. }
  17. func NewMissingAggregation() MissingAggregation {
  18. a := MissingAggregation{
  19. subAggregations: make(map[string]Aggregation),
  20. }
  21. return a
  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. func (a MissingAggregation) Source() interface{} {
  32. // Example:
  33. // {
  34. // "aggs" : {
  35. // "products_without_a_price" : {
  36. // "missing" : { "field" : "price" }
  37. // }
  38. // }
  39. // }
  40. // This method returns only the { "missing" : { ... } } part.
  41. source := make(map[string]interface{})
  42. opts := make(map[string]interface{})
  43. source["missing"] = opts
  44. if a.field != "" {
  45. opts["field"] = a.field
  46. }
  47. // AggregationBuilder (SubAggregations)
  48. if len(a.subAggregations) > 0 {
  49. aggsMap := make(map[string]interface{})
  50. source["aggregations"] = aggsMap
  51. for name, aggregate := range a.subAggregations {
  52. aggsMap[name] = aggregate.Source()
  53. }
  54. }
  55. return source
  56. }