iprange-aggregation.asciidoc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. [[search-aggregations-bucket-iprange-aggregation]]
  2. === IP range aggregation
  3. ++++
  4. <titleabbrev>IP range</titleabbrev>
  5. ++++
  6. Just like the dedicated <<search-aggregations-bucket-daterange-aggregation,date>> range aggregation, there is also a dedicated range aggregation for IP typed fields:
  7. Example:
  8. [source,console,id=ip-range-example]
  9. --------------------------------------------------
  10. GET /ip_addresses/_search
  11. {
  12. "size": 10,
  13. "aggs": {
  14. "ip_ranges": {
  15. "ip_range": {
  16. "field": "ip",
  17. "ranges": [
  18. { "to": "10.0.0.5" },
  19. { "from": "10.0.0.5" }
  20. ]
  21. }
  22. }
  23. }
  24. }
  25. --------------------------------------------------
  26. // TEST[setup:iprange]
  27. Response:
  28. [source,console-result]
  29. --------------------------------------------------
  30. {
  31. ...
  32. "aggregations": {
  33. "ip_ranges": {
  34. "buckets": [
  35. {
  36. "key": "*-10.0.0.5",
  37. "to": "10.0.0.5",
  38. "doc_count": 10
  39. },
  40. {
  41. "key": "10.0.0.5-*",
  42. "from": "10.0.0.5",
  43. "doc_count": 260
  44. }
  45. ]
  46. }
  47. }
  48. }
  49. --------------------------------------------------
  50. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  51. IP ranges can also be defined as CIDR masks:
  52. [source,console,id=ip-range-cidr-example]
  53. --------------------------------------------------
  54. GET /ip_addresses/_search
  55. {
  56. "size": 0,
  57. "aggs": {
  58. "ip_ranges": {
  59. "ip_range": {
  60. "field": "ip",
  61. "ranges": [
  62. { "mask": "10.0.0.0/25" },
  63. { "mask": "10.0.0.127/25" }
  64. ]
  65. }
  66. }
  67. }
  68. }
  69. --------------------------------------------------
  70. // TEST[setup:iprange]
  71. Response:
  72. [source,console-result]
  73. --------------------------------------------------
  74. {
  75. ...
  76. "aggregations": {
  77. "ip_ranges": {
  78. "buckets": [
  79. {
  80. "key": "10.0.0.0/25",
  81. "from": "10.0.0.0",
  82. "to": "10.0.0.128",
  83. "doc_count": 128
  84. },
  85. {
  86. "key": "10.0.0.127/25",
  87. "from": "10.0.0.0",
  88. "to": "10.0.0.128",
  89. "doc_count": 128
  90. }
  91. ]
  92. }
  93. }
  94. }
  95. --------------------------------------------------
  96. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  97. ==== Keyed Response
  98. 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:
  99. [source,console,id=ip-range-keyed-example]
  100. --------------------------------------------------
  101. GET /ip_addresses/_search
  102. {
  103. "size": 0,
  104. "aggs": {
  105. "ip_ranges": {
  106. "ip_range": {
  107. "field": "ip",
  108. "ranges": [
  109. { "to": "10.0.0.5" },
  110. { "from": "10.0.0.5" }
  111. ],
  112. "keyed": true
  113. }
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. // TEST[setup:iprange]
  119. Response:
  120. [source,console-result]
  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,console,id=ip-range-keyed-customized-keys-example]
  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. // TEST[setup:iprange]
  162. Response:
  163. [source,console-result]
  164. --------------------------------------------------
  165. {
  166. ...
  167. "aggregations": {
  168. "ip_ranges": {
  169. "buckets": {
  170. "infinity": {
  171. "to": "10.0.0.5",
  172. "doc_count": 10
  173. },
  174. "and-beyond": {
  175. "from": "10.0.0.5",
  176. "doc_count": 260
  177. }
  178. }
  179. }
  180. }
  181. }
  182. --------------------------------------------------
  183. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]