iprange-aggregation.asciidoc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. {
  8. "aggs" : {
  9. "ip_ranges" : {
  10. "ip_range" : {
  11. "field" : "ip",
  12. "ranges" : [
  13. { "to" : "10.0.0.5" },
  14. { "from" : "10.0.0.5" }
  15. ]
  16. }
  17. }
  18. }
  19. }
  20. --------------------------------------------------
  21. Response:
  22. [source,js]
  23. --------------------------------------------------
  24. {
  25. ...
  26. "aggregations": {
  27. "ip_ranges": {
  28. "buckets" : [
  29. {
  30. "to": "10.0.0.5",
  31. "doc_count": 4
  32. },
  33. {
  34. "from": "10.0.0.5",
  35. "doc_count": 6
  36. }
  37. ]
  38. }
  39. }
  40. }
  41. --------------------------------------------------
  42. IP ranges can also be defined as CIDR masks:
  43. [source,js]
  44. --------------------------------------------------
  45. {
  46. "aggs" : {
  47. "ip_ranges" : {
  48. "ip_range" : {
  49. "field" : "ip",
  50. "ranges" : [
  51. { "mask" : "10.0.0.0/25" },
  52. { "mask" : "10.0.0.127/25" }
  53. ]
  54. }
  55. }
  56. }
  57. }
  58. --------------------------------------------------
  59. Response:
  60. [source,js]
  61. --------------------------------------------------
  62. {
  63. "aggregations": {
  64. "ip_ranges": {
  65. "buckets": [
  66. {
  67. "key": "10.0.0.0/25",
  68. "from": "10.0.0.0",
  69. "to": "10.0.0.127",
  70. "doc_count": 127
  71. },
  72. {
  73. "key": "10.0.0.127/25",
  74. "from": "10.0.0.0",
  75. "to": "10.0.0.127",
  76. "doc_count": 127
  77. }
  78. ]
  79. }
  80. }
  81. }
  82. --------------------------------------------------
  83. ==== Keyed Response
  84. 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:
  85. [source,js]
  86. --------------------------------------------------
  87. {
  88. "aggs": {
  89. "ip_ranges": {
  90. "ip_range": {
  91. "field": "remote_ip",
  92. "ranges": [
  93. { "to" : "10.0.0.5" },
  94. { "from" : "10.0.0.5" }
  95. ],
  96. "keyed": true
  97. }
  98. }
  99. }
  100. }
  101. --------------------------------------------------
  102. Response:
  103. [source,js]
  104. --------------------------------------------------
  105. {
  106. ...
  107. "aggregations": {
  108. "ip_ranges": {
  109. "buckets": {
  110. "*-10.0.0.5": {
  111. "to": "10.0.0.5",
  112. "doc_count": 1462
  113. },
  114. "10.0.0.5-*": {
  115. "from": "10.0.0.5",
  116. "doc_count": 50000
  117. }
  118. }
  119. }
  120. }
  121. }
  122. --------------------------------------------------
  123. It is also possible to customize the key for each range:
  124. [source,js]
  125. --------------------------------------------------
  126. {
  127. "aggs": {
  128. "ip_ranges": {
  129. "ip_range": {
  130. "field": "remote_ip",
  131. "ranges": [
  132. { "key": "infinity", "to" : "10.0.0.5" },
  133. { "key": "and-beyond", "from" : "10.0.0.5" }
  134. ],
  135. "keyed": true
  136. }
  137. }
  138. }
  139. }
  140. --------------------------------------------------
  141. Response:
  142. [source,js]
  143. --------------------------------------------------
  144. {
  145. ...
  146. "aggregations": {
  147. "ip_ranges": {
  148. "buckets": {
  149. "infinity": {
  150. "to": "10.0.0.5",
  151. "doc_count": 1462
  152. },
  153. "and-beyond": {
  154. "from": "10.0.0.5",
  155. "doc_count": 50000
  156. }
  157. }
  158. }
  159. }
  160. }
  161. --------------------------------------------------