search_aggs_bucket_ip_range.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // IPRangeAggregation is a range aggregation that is dedicated for
  6. // IP addresses.
  7. //
  8. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-iprange-aggregation.html
  9. type IPRangeAggregation struct {
  10. field string
  11. subAggregations map[string]Aggregation
  12. meta map[string]interface{}
  13. keyed *bool
  14. entries []IPRangeAggregationEntry
  15. }
  16. type IPRangeAggregationEntry struct {
  17. Key string
  18. Mask string
  19. From string
  20. To string
  21. }
  22. func NewIPRangeAggregation() *IPRangeAggregation {
  23. return &IPRangeAggregation{
  24. subAggregations: make(map[string]Aggregation),
  25. entries: make([]IPRangeAggregationEntry, 0),
  26. }
  27. }
  28. func (a *IPRangeAggregation) Field(field string) *IPRangeAggregation {
  29. a.field = field
  30. return a
  31. }
  32. func (a *IPRangeAggregation) SubAggregation(name string, subAggregation Aggregation) *IPRangeAggregation {
  33. a.subAggregations[name] = subAggregation
  34. return a
  35. }
  36. // Meta sets the meta data to be included in the aggregation response.
  37. func (a *IPRangeAggregation) Meta(metaData map[string]interface{}) *IPRangeAggregation {
  38. a.meta = metaData
  39. return a
  40. }
  41. func (a *IPRangeAggregation) Keyed(keyed bool) *IPRangeAggregation {
  42. a.keyed = &keyed
  43. return a
  44. }
  45. func (a *IPRangeAggregation) AddMaskRange(mask string) *IPRangeAggregation {
  46. a.entries = append(a.entries, IPRangeAggregationEntry{Mask: mask})
  47. return a
  48. }
  49. func (a *IPRangeAggregation) AddMaskRangeWithKey(key, mask string) *IPRangeAggregation {
  50. a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, Mask: mask})
  51. return a
  52. }
  53. func (a *IPRangeAggregation) AddRange(from, to string) *IPRangeAggregation {
  54. a.entries = append(a.entries, IPRangeAggregationEntry{From: from, To: to})
  55. return a
  56. }
  57. func (a *IPRangeAggregation) AddRangeWithKey(key, from, to string) *IPRangeAggregation {
  58. a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: from, To: to})
  59. return a
  60. }
  61. func (a *IPRangeAggregation) AddUnboundedTo(from string) *IPRangeAggregation {
  62. a.entries = append(a.entries, IPRangeAggregationEntry{From: from, To: ""})
  63. return a
  64. }
  65. func (a *IPRangeAggregation) AddUnboundedToWithKey(key, from string) *IPRangeAggregation {
  66. a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: from, To: ""})
  67. return a
  68. }
  69. func (a *IPRangeAggregation) AddUnboundedFrom(to string) *IPRangeAggregation {
  70. a.entries = append(a.entries, IPRangeAggregationEntry{From: "", To: to})
  71. return a
  72. }
  73. func (a *IPRangeAggregation) AddUnboundedFromWithKey(key, to string) *IPRangeAggregation {
  74. a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: "", To: to})
  75. return a
  76. }
  77. func (a *IPRangeAggregation) Lt(to string) *IPRangeAggregation {
  78. a.entries = append(a.entries, IPRangeAggregationEntry{From: "", To: to})
  79. return a
  80. }
  81. func (a *IPRangeAggregation) LtWithKey(key, to string) *IPRangeAggregation {
  82. a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: "", To: to})
  83. return a
  84. }
  85. func (a *IPRangeAggregation) Between(from, to string) *IPRangeAggregation {
  86. a.entries = append(a.entries, IPRangeAggregationEntry{From: from, To: to})
  87. return a
  88. }
  89. func (a *IPRangeAggregation) BetweenWithKey(key, from, to string) *IPRangeAggregation {
  90. a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: from, To: to})
  91. return a
  92. }
  93. func (a *IPRangeAggregation) Gt(from string) *IPRangeAggregation {
  94. a.entries = append(a.entries, IPRangeAggregationEntry{From: from, To: ""})
  95. return a
  96. }
  97. func (a *IPRangeAggregation) GtWithKey(key, from string) *IPRangeAggregation {
  98. a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: from, To: ""})
  99. return a
  100. }
  101. func (a *IPRangeAggregation) Source() (interface{}, error) {
  102. // Example:
  103. // {
  104. // "aggs" : {
  105. // "range" : {
  106. // "ip_range": {
  107. // "field": "ip",
  108. // "ranges": [
  109. // { "to": "10.0.0.5" },
  110. // { "from": "10.0.0.5" }
  111. // ]
  112. // }
  113. // }
  114. // }
  115. // }
  116. // }
  117. //
  118. // This method returns only the { "ip_range" : { ... } } part.
  119. source := make(map[string]interface{})
  120. opts := make(map[string]interface{})
  121. source["ip_range"] = opts
  122. // ValuesSourceAggregationBuilder
  123. if a.field != "" {
  124. opts["field"] = a.field
  125. }
  126. if a.keyed != nil {
  127. opts["keyed"] = *a.keyed
  128. }
  129. var ranges []interface{}
  130. for _, ent := range a.entries {
  131. r := make(map[string]interface{})
  132. if ent.Key != "" {
  133. r["key"] = ent.Key
  134. }
  135. if ent.Mask != "" {
  136. r["mask"] = ent.Mask
  137. } else {
  138. if ent.From != "" {
  139. r["from"] = ent.From
  140. }
  141. if ent.To != "" {
  142. r["to"] = ent.To
  143. }
  144. }
  145. ranges = append(ranges, r)
  146. }
  147. opts["ranges"] = ranges
  148. // AggregationBuilder (SubAggregations)
  149. if len(a.subAggregations) > 0 {
  150. aggsMap := make(map[string]interface{})
  151. source["aggregations"] = aggsMap
  152. for name, aggregate := range a.subAggregations {
  153. src, err := aggregate.Source()
  154. if err != nil {
  155. return nil, err
  156. }
  157. aggsMap[name] = src
  158. }
  159. }
  160. // Add Meta data if available
  161. if len(a.meta) > 0 {
  162. source["meta"] = a.meta
  163. }
  164. return source, nil
  165. }