iprange-aggregation.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/data/_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. "to": "10.0.0.5",
  35. "doc_count": 10
  36. },
  37. {
  38. "from": "10.0.0.5",
  39. "doc_count": 260
  40. }
  41. ]
  42. }
  43. }
  44. }
  45. --------------------------------------------------
  46. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  47. IP ranges can also be defined as CIDR masks:
  48. [source,js]
  49. --------------------------------------------------
  50. GET /ip_addresses/data/_search
  51. {
  52. "size": 0,
  53. "aggs" : {
  54. "ip_ranges" : {
  55. "ip_range" : {
  56. "field" : "ip",
  57. "ranges" : [
  58. { "mask" : "10.0.0.0/25" },
  59. { "mask" : "10.0.0.127/25" }
  60. ]
  61. }
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. // CONSOLE
  67. // TEST[setup:iprange]
  68. Response:
  69. [source,js]
  70. --------------------------------------------------
  71. {
  72. ...
  73. "aggregations": {
  74. "ip_ranges": {
  75. "buckets": [
  76. {
  77. "key": "10.0.0.0/25",
  78. "from": "10.0.0.0",
  79. "to": "10.0.0.128",
  80. "doc_count": 128
  81. },
  82. {
  83. "key": "10.0.0.127/25",
  84. "from": "10.0.0.0",
  85. "to": "10.0.0.128",
  86. "doc_count": 128
  87. }
  88. ]
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  94. ==== Keyed Response
  95. 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:
  96. [source,js]
  97. --------------------------------------------------
  98. GET /ip_addresses/data/_search
  99. {
  100. "size": 0,
  101. "aggs": {
  102. "ip_ranges": {
  103. "ip_range": {
  104. "field": "ip",
  105. "ranges": [
  106. { "to" : "10.0.0.5" },
  107. { "from" : "10.0.0.5" }
  108. ],
  109. "keyed": true
  110. }
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // CONSOLE
  116. // TEST[setup:iprange]
  117. Response:
  118. [source,js]
  119. --------------------------------------------------
  120. {
  121. ...
  122. "aggregations": {
  123. "ip_ranges": {
  124. "buckets": {
  125. "*-10.0.0.5": {
  126. "to": "10.0.0.5",
  127. "doc_count": 10
  128. },
  129. "10.0.0.5-*": {
  130. "from": "10.0.0.5",
  131. "doc_count": 260
  132. }
  133. }
  134. }
  135. }
  136. }
  137. --------------------------------------------------
  138. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  139. It is also possible to customize the key for each range:
  140. [source,js]
  141. --------------------------------------------------
  142. GET /ip_addresses/data/_search
  143. {
  144. "size": 0,
  145. "aggs": {
  146. "ip_ranges": {
  147. "ip_range": {
  148. "field": "ip",
  149. "ranges": [
  150. { "key": "infinity", "to" : "10.0.0.5" },
  151. { "key": "and-beyond", "from" : "10.0.0.5" }
  152. ],
  153. "keyed": true
  154. }
  155. }
  156. }
  157. }
  158. --------------------------------------------------
  159. // CONSOLE
  160. // TEST[setup:iprange]
  161. Response:
  162. [source,js]
  163. --------------------------------------------------
  164. {
  165. ...
  166. "aggregations": {
  167. "ip_ranges": {
  168. "buckets": {
  169. "infinity": {
  170. "to": "10.0.0.5",
  171. "doc_count": 10
  172. },
  173. "and-beyond": {
  174. "from": "10.0.0.5",
  175. "doc_count": 260
  176. }
  177. }
  178. }
  179. }
  180. }
  181. --------------------------------------------------
  182. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]