iprange-aggregation.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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,console]
  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. // TEST[setup:iprange]
  24. Response:
  25. [source,js]
  26. --------------------------------------------------
  27. {
  28. ...
  29. "aggregations": {
  30. "ip_ranges": {
  31. "buckets" : [
  32. {
  33. "key": "*-10.0.0.5",
  34. "to": "10.0.0.5",
  35. "doc_count": 10
  36. },
  37. {
  38. "key": "10.0.0.5-*",
  39. "from": "10.0.0.5",
  40. "doc_count": 260
  41. }
  42. ]
  43. }
  44. }
  45. }
  46. --------------------------------------------------
  47. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  48. IP ranges can also be defined as CIDR masks:
  49. [source,console]
  50. --------------------------------------------------
  51. GET /ip_addresses/_search
  52. {
  53. "size": 0,
  54. "aggs" : {
  55. "ip_ranges" : {
  56. "ip_range" : {
  57. "field" : "ip",
  58. "ranges" : [
  59. { "mask" : "10.0.0.0/25" },
  60. { "mask" : "10.0.0.127/25" }
  61. ]
  62. }
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  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,console]
  97. --------------------------------------------------
  98. GET /ip_addresses/_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. // TEST[setup:iprange]
  116. Response:
  117. [source,js]
  118. --------------------------------------------------
  119. {
  120. ...
  121. "aggregations": {
  122. "ip_ranges": {
  123. "buckets": {
  124. "*-10.0.0.5": {
  125. "to": "10.0.0.5",
  126. "doc_count": 10
  127. },
  128. "10.0.0.5-*": {
  129. "from": "10.0.0.5",
  130. "doc_count": 260
  131. }
  132. }
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  138. It is also possible to customize the key for each range:
  139. [source,console]
  140. --------------------------------------------------
  141. GET /ip_addresses/_search
  142. {
  143. "size": 0,
  144. "aggs": {
  145. "ip_ranges": {
  146. "ip_range": {
  147. "field": "ip",
  148. "ranges": [
  149. { "key": "infinity", "to" : "10.0.0.5" },
  150. { "key": "and-beyond", "from" : "10.0.0.5" }
  151. ],
  152. "keyed": true
  153. }
  154. }
  155. }
  156. }
  157. --------------------------------------------------
  158. // TEST[setup:iprange]
  159. Response:
  160. [source,js]
  161. --------------------------------------------------
  162. {
  163. ...
  164. "aggregations": {
  165. "ip_ranges": {
  166. "buckets": {
  167. "infinity": {
  168. "to": "10.0.0.5",
  169. "doc_count": 10
  170. },
  171. "and-beyond": {
  172. "from": "10.0.0.5",
  173. "doc_count": 260
  174. }
  175. }
  176. }
  177. }
  178. }
  179. --------------------------------------------------
  180. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]