iprange-aggregation.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. [[search-aggregations-bucket-iprange-aggregation]]
  2. === IP Range Aggregation
  3. Just like the dedicated <<search-aggregations-bucket-daterange-aggregation,date>> range aggregation, there is also a dedicated range aggregation for IP typed fields:
  4. Example:
  5. [source,js]
  6. --------------------------------------------------
  7. GET /ip_addresses/_search
  8. {
  9. "size": 10,
  10. "aggs" : {
  11. "ip_ranges" : {
  12. "ip_range" : {
  13. "field" : "ip",
  14. "ranges" : [
  15. { "to" : "10.0.0.5" },
  16. { "from" : "10.0.0.5" }
  17. ]
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. // CONSOLE
  24. // TEST[setup:iprange]
  25. Response:
  26. [source,js]
  27. --------------------------------------------------
  28. {
  29. ...
  30. "aggregations": {
  31. "ip_ranges": {
  32. "buckets" : [
  33. {
  34. "key": "*-10.0.0.5",
  35. "to": "10.0.0.5",
  36. "doc_count": 10
  37. },
  38. {
  39. "key": "10.0.0.5-*",
  40. "from": "10.0.0.5",
  41. "doc_count": 260
  42. }
  43. ]
  44. }
  45. }
  46. }
  47. --------------------------------------------------
  48. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  49. IP ranges can also be defined as CIDR masks:
  50. [source,js]
  51. --------------------------------------------------
  52. GET /ip_addresses/_search
  53. {
  54. "size": 0,
  55. "aggs" : {
  56. "ip_ranges" : {
  57. "ip_range" : {
  58. "field" : "ip",
  59. "ranges" : [
  60. { "mask" : "10.0.0.0/25" },
  61. { "mask" : "10.0.0.127/25" }
  62. ]
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. // CONSOLE
  69. // TEST[setup:iprange]
  70. Response:
  71. [source,js]
  72. --------------------------------------------------
  73. {
  74. ...
  75. "aggregations": {
  76. "ip_ranges": {
  77. "buckets": [
  78. {
  79. "key": "10.0.0.0/25",
  80. "from": "10.0.0.0",
  81. "to": "10.0.0.128",
  82. "doc_count": 128
  83. },
  84. {
  85. "key": "10.0.0.127/25",
  86. "from": "10.0.0.0",
  87. "to": "10.0.0.128",
  88. "doc_count": 128
  89. }
  90. ]
  91. }
  92. }
  93. }
  94. --------------------------------------------------
  95. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  96. ==== Keyed Response
  97. Setting the `keyed` flag to `true` will associate a unique string key with each bucket and return the ranges as a hash rather than an array:
  98. [source,js]
  99. --------------------------------------------------
  100. GET /ip_addresses/_search
  101. {
  102. "size": 0,
  103. "aggs": {
  104. "ip_ranges": {
  105. "ip_range": {
  106. "field": "ip",
  107. "ranges": [
  108. { "to" : "10.0.0.5" },
  109. { "from" : "10.0.0.5" }
  110. ],
  111. "keyed": true
  112. }
  113. }
  114. }
  115. }
  116. --------------------------------------------------
  117. // CONSOLE
  118. // TEST[setup:iprange]
  119. Response:
  120. [source,js]
  121. --------------------------------------------------
  122. {
  123. ...
  124. "aggregations": {
  125. "ip_ranges": {
  126. "buckets": {
  127. "*-10.0.0.5": {
  128. "to": "10.0.0.5",
  129. "doc_count": 10
  130. },
  131. "10.0.0.5-*": {
  132. "from": "10.0.0.5",
  133. "doc_count": 260
  134. }
  135. }
  136. }
  137. }
  138. }
  139. --------------------------------------------------
  140. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  141. It is also possible to customize the key for each range:
  142. [source,js]
  143. --------------------------------------------------
  144. GET /ip_addresses/_search
  145. {
  146. "size": 0,
  147. "aggs": {
  148. "ip_ranges": {
  149. "ip_range": {
  150. "field": "ip",
  151. "ranges": [
  152. { "key": "infinity", "to" : "10.0.0.5" },
  153. { "key": "and-beyond", "from" : "10.0.0.5" }
  154. ],
  155. "keyed": true
  156. }
  157. }
  158. }
  159. }
  160. --------------------------------------------------
  161. // CONSOLE
  162. // TEST[setup:iprange]
  163. Response:
  164. [source,js]
  165. --------------------------------------------------
  166. {
  167. ...
  168. "aggregations": {
  169. "ip_ranges": {
  170. "buckets": {
  171. "infinity": {
  172. "to": "10.0.0.5",
  173. "doc_count": 10
  174. },
  175. "and-beyond": {
  176. "from": "10.0.0.5",
  177. "doc_count": 260
  178. }
  179. }
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]