ipprefix-aggregation.asciidoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. [[search-aggregations-bucket-ipprefix-aggregation]]
  2. === IP prefix aggregation
  3. ++++
  4. <titleabbrev>IP prefix</titleabbrev>
  5. ++++
  6. A bucket aggregation that groups documents based on the network or sub-network of an IP address. An IP address consists of two groups of bits: the most significant bits which represent the network prefix, and the least significant bits which represent the host.
  7. [[ipprefix-agg-ex]]
  8. ==== Example
  9. For example, consider the following index:
  10. [source,console]
  11. ----------------------------------------------
  12. PUT network-traffic
  13. {
  14. "mappings": {
  15. "properties": {
  16. "ipv4": { "type": "ip" },
  17. "ipv6": { "type": "ip" }
  18. }
  19. }
  20. }
  21. POST /network-traffic/_bulk?refresh
  22. {"index":{"_id":0}}
  23. {"ipv4":"192.168.1.10","ipv6":"2001:db8:a4f8:112a:6001:0:12:7f10"}
  24. {"index":{"_id":1}}
  25. {"ipv4":"192.168.1.12","ipv6":"2001:db8:a4f8:112a:6001:0:12:7f12"}
  26. {"index":{"_id":2}}
  27. { "ipv4":"192.168.1.33","ipv6":"2001:db8:a4f8:112a:6001:0:12:7f33"}
  28. {"index":{"_id":3}}
  29. {"ipv4":"192.168.1.10","ipv6":"2001:db8:a4f8:112a:6001:0:12:7f10"}
  30. {"index":{"_id":4}}
  31. {"ipv4":"192.168.2.41","ipv6":"2001:db8:a4f8:112c:6001:0:12:7f41"}
  32. {"index":{"_id":5}}
  33. {"ipv4":"192.168.2.10","ipv6":"2001:db8:a4f8:112c:6001:0:12:7f10"}
  34. {"index":{"_id":6}}
  35. {"ipv4":"192.168.2.23","ipv6":"2001:db8:a4f8:112c:6001:0:12:7f23"}
  36. {"index":{"_id":7}}
  37. {"ipv4":"192.168.3.201","ipv6":"2001:db8:a4f8:114f:6001:0:12:7201"}
  38. {"index":{"_id":8}}
  39. {"ipv4":"192.168.3.107","ipv6":"2001:db8:a4f8:114f:6001:0:12:7307"}
  40. ----------------------------------------------
  41. // TESTSETUP
  42. The following aggregation groups documents into buckets. Each bucket identifies a different sub-network. The sub-network is calculated by applying a netmask with prefix length of `24` to each IP address in the `ipv4` field:
  43. [source,console,id=ip-prefix-ipv4-example]
  44. --------------------------------------------------
  45. GET /network-traffic/_search
  46. {
  47. "size": 0,
  48. "aggs": {
  49. "ipv4-subnets": {
  50. "ip_prefix": {
  51. "field": "ipv4",
  52. "prefix_length": 24
  53. }
  54. }
  55. }
  56. }
  57. --------------------------------------------------
  58. // TEST
  59. Response:
  60. [source,console-result]
  61. --------------------------------------------------
  62. {
  63. ...
  64. "aggregations": {
  65. "ipv4-subnets": {
  66. "buckets": [
  67. {
  68. "key": "192.168.1.0",
  69. "is_ipv6": false,
  70. "doc_count": 4,
  71. "prefix_length": 24,
  72. "netmask": "255.255.255.0"
  73. },
  74. {
  75. "key": "192.168.2.0",
  76. "is_ipv6": false,
  77. "doc_count": 3,
  78. "prefix_length": 24,
  79. "netmask": "255.255.255.0"
  80. },
  81. {
  82. "key": "192.168.3.0",
  83. "is_ipv6": false,
  84. "doc_count": 2,
  85. "prefix_length": 24,
  86. "netmask": "255.255.255.0"
  87. }
  88. ]
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  94. To aggregate IPv6 addresses, set `is_ipv6` to `true`.
  95. [source,console,id=ip-prefix-ipv6-example]
  96. --------------------------------------------------
  97. GET /network-traffic/_search
  98. {
  99. "size": 0,
  100. "aggs": {
  101. "ipv6-subnets": {
  102. "ip_prefix": {
  103. "field": "ipv6",
  104. "prefix_length": 64,
  105. "is_ipv6": true
  106. }
  107. }
  108. }
  109. }
  110. --------------------------------------------------
  111. // TEST
  112. If `is_ipv6` is `true`, the response doesn't include a `netmask` for each bucket.
  113. [source,console-result]
  114. --------------------------------------------------
  115. {
  116. ...
  117. "aggregations": {
  118. "ipv6-subnets": {
  119. "buckets": [
  120. {
  121. "key": "2001:db8:a4f8:112a::",
  122. "is_ipv6": true,
  123. "doc_count": 4,
  124. "prefix_length": 64
  125. },
  126. {
  127. "key": "2001:db8:a4f8:112c::",
  128. "is_ipv6": true,
  129. "doc_count": 3,
  130. "prefix_length": 64
  131. },
  132. {
  133. "key": "2001:db8:a4f8:114f::",
  134. "is_ipv6": true,
  135. "doc_count": 2,
  136. "prefix_length": 64
  137. }
  138. ]
  139. }
  140. }
  141. }
  142. --------------------------------------------------
  143. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  144. [role="child_attributes"]
  145. [[ip-prefix-agg-params]]
  146. ==== Parameters
  147. `field`::
  148. (Required, string)
  149. The document IP address field to aggregate on. The field mapping type must be <<ip,`ip`>>.
  150. `prefix_length`::
  151. (Required, integer)
  152. Length of the network prefix. For IPv4 addresses, the accepted range is `[0, 32]`. For IPv6 addresses, the accepted range is `[0, 128]`.
  153. `is_ipv6`::
  154. (Optional, boolean)
  155. Defines whether the prefix applies to IPv6 addresses. Just specifying the `prefix_length` parameter is not enough to know if an IP prefix applies to IPv4 or IPv6 addresses. Defaults to `false`.
  156. `append_prefix_length`::
  157. (Optional, boolean)
  158. Defines whether the prefix length is appended to IP address keys in the response. Defaults to `false`.
  159. `keyed`::
  160. (Optional, boolean)
  161. Defines whether buckets are returned as a hash rather than an array in the response. Defaults to `false`.
  162. `min_doc_count`::
  163. (Optional, integer)
  164. Defines the minimum number of documents for buckets to be included in the response. Defaults to `1`.
  165. [[ipprefix-agg-response]]
  166. ==== Response body
  167. `key`::
  168. (string)
  169. The IPv6 or IPv4 subnet.
  170. `prefix_length`::
  171. (integer)
  172. The length of the prefix used to aggregate the bucket.
  173. `doc_count`::
  174. (integer)
  175. Number of documents matching a specific IP prefix.
  176. `is_ipv6`::
  177. (boolean)
  178. Defines whether the netmask is an IPv6 netmask.
  179. `netmask`::
  180. (string)
  181. The IPv4 netmask. If `is_ipv6` is `true` in the request, this field is missing in the response.
  182. [[ipprefix-agg-keyed-response]]
  183. ==== Keyed Response
  184. Set the `keyed` flag of `true` to associate an unique IP address key with each bucket and return sub-networks as a hash rather than an array.
  185. Example:
  186. [source,console,id=ip-prefix-keyed-example]
  187. --------------------------------------------------
  188. GET /network-traffic/_search
  189. {
  190. "size": 0,
  191. "aggs": {
  192. "ipv4-subnets": {
  193. "ip_prefix": {
  194. "field": "ipv4",
  195. "prefix_length": 24,
  196. "keyed": true
  197. }
  198. }
  199. }
  200. }
  201. --------------------------------------------------
  202. // TEST
  203. Response:
  204. [source,console-result]
  205. --------------------------------------------------
  206. {
  207. ...
  208. "aggregations": {
  209. "ipv4-subnets": {
  210. "buckets": {
  211. "192.168.1.0": {
  212. "is_ipv6": false,
  213. "doc_count": 4,
  214. "prefix_length": 24,
  215. "netmask": "255.255.255.0"
  216. },
  217. "192.168.2.0": {
  218. "is_ipv6": false,
  219. "doc_count": 3,
  220. "prefix_length": 24,
  221. "netmask": "255.255.255.0"
  222. },
  223. "192.168.3.0": {
  224. "is_ipv6": false,
  225. "doc_count": 2,
  226. "prefix_length": 24,
  227. "netmask": "255.255.255.0"
  228. }
  229. }
  230. }
  231. }
  232. }
  233. --------------------------------------------------
  234. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  235. [[ipprefix-agg-append-prefix-length]]
  236. ==== Append the prefix length to the IP address key
  237. Set the `append_prefix_length` flag to `true` to catenate IP address keys with the prefix length of the sub-network.
  238. Example:
  239. [source,console,id=ip-prefix-append-prefix-len-example]
  240. --------------------------------------------------
  241. GET /network-traffic/_search
  242. {
  243. "size": 0,
  244. "aggs": {
  245. "ipv4-subnets": {
  246. "ip_prefix": {
  247. "field": "ipv4",
  248. "prefix_length": 24,
  249. "append_prefix_length": true
  250. }
  251. }
  252. }
  253. }
  254. --------------------------------------------------
  255. // TEST
  256. Response:
  257. [source,console-result]
  258. --------------------------------------------------
  259. {
  260. ...
  261. "aggregations": {
  262. "ipv4-subnets": {
  263. "buckets": [
  264. {
  265. "key": "192.168.1.0/24",
  266. "is_ipv6": false,
  267. "doc_count": 4,
  268. "prefix_length": 24,
  269. "netmask": "255.255.255.0"
  270. },
  271. {
  272. "key": "192.168.2.0/24",
  273. "is_ipv6": false,
  274. "doc_count": 3,
  275. "prefix_length": 24,
  276. "netmask": "255.255.255.0"
  277. },
  278. {
  279. "key": "192.168.3.0/24",
  280. "is_ipv6": false,
  281. "doc_count": 2,
  282. "prefix_length": 24,
  283. "netmask": "255.255.255.0"
  284. }
  285. ]
  286. }
  287. }
  288. }
  289. --------------------------------------------------
  290. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  291. [[ipprefix-agg-min-doc-count]]
  292. ==== Minimum document count
  293. Use the `min_doc_count` parameter to only return buckets with a minimum number of documents.
  294. [source,console,id=ip-prefix-min-doc-count-example]
  295. --------------------------------------------------
  296. GET /network-traffic/_search
  297. {
  298. "size": 0,
  299. "aggs": {
  300. "ipv4-subnets": {
  301. "ip_prefix": {
  302. "field": "ipv4",
  303. "prefix_length": 24,
  304. "min_doc_count": 3
  305. }
  306. }
  307. }
  308. }
  309. --------------------------------------------------
  310. // TEST
  311. Response:
  312. [source,console-result]
  313. --------------------------------------------------
  314. {
  315. ...
  316. "aggregations": {
  317. "ipv4-subnets": {
  318. "buckets": [
  319. {
  320. "key": "192.168.1.0",
  321. "is_ipv6": false,
  322. "doc_count": 4,
  323. "prefix_length": 24,
  324. "netmask": "255.255.255.0"
  325. },
  326. {
  327. "key": "192.168.2.0",
  328. "is_ipv6": false,
  329. "doc_count": 3,
  330. "prefix_length": 24,
  331. "netmask": "255.255.255.0"
  332. }
  333. ]
  334. }
  335. }
  336. }
  337. --------------------------------------------------
  338. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]