IP範囲の集約

専用の date 範囲集約と同様に、IP型フィールド用の専用範囲集約もあります:

例:

Python

  1. resp = client.search(
  2. index="ip_addresses",
  3. size=10,
  4. aggs={
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. {
  10. "to": "10.0.0.5"
  11. },
  12. {
  13. "from": "10.0.0.5"
  14. }
  15. ]
  16. }
  17. }
  18. },
  19. )
  20. print(resp)

Ruby

  1. response = client.search(
  2. index: 'ip_addresses',
  3. body: {
  4. size: 10,
  5. aggregations: {
  6. ip_ranges: {
  7. ip_range: {
  8. field: 'ip',
  9. ranges: [
  10. {
  11. to: '10.0.0.5'
  12. },
  13. {
  14. from: '10.0.0.5'
  15. }
  16. ]
  17. }
  18. }
  19. }
  20. }
  21. )
  22. puts response

Js

  1. const response = await client.search({
  2. index: "ip_addresses",
  3. size: 10,
  4. aggs: {
  5. ip_ranges: {
  6. ip_range: {
  7. field: "ip",
  8. ranges: [
  9. {
  10. to: "10.0.0.5",
  11. },
  12. {
  13. from: "10.0.0.5",
  14. },
  15. ],
  16. },
  17. },
  18. },
  19. });
  20. console.log(response);

コンソール

  1. GET /ip_addresses/_search
  2. {
  3. "size": 10,
  4. "aggs": {
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. { "to": "10.0.0.5" },
  10. { "from": "10.0.0.5" }
  11. ]
  12. }
  13. }
  14. }
  15. }

応答:

コンソール結果

  1. {
  2. ...
  3. "aggregations": {
  4. "ip_ranges": {
  5. "buckets": [
  6. {
  7. "key": "*-10.0.0.5",
  8. "to": "10.0.0.5",
  9. "doc_count": 10
  10. },
  11. {
  12. "key": "10.0.0.5-*",
  13. "from": "10.0.0.5",
  14. "doc_count": 260
  15. }
  16. ]
  17. }
  18. }
  19. }

IP範囲はCIDRマスクとしても定義できます:

Python

  1. resp = client.search(
  2. index="ip_addresses",
  3. size=0,
  4. aggs={
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. {
  10. "mask": "10.0.0.0/25"
  11. },
  12. {
  13. "mask": "10.0.0.127/25"
  14. }
  15. ]
  16. }
  17. }
  18. },
  19. )
  20. print(resp)

Ruby

  1. response = client.search(
  2. index: 'ip_addresses',
  3. body: {
  4. size: 0,
  5. aggregations: {
  6. ip_ranges: {
  7. ip_range: {
  8. field: 'ip',
  9. ranges: [
  10. {
  11. mask: '10.0.0.0/25'
  12. },
  13. {
  14. mask: '10.0.0.127/25'
  15. }
  16. ]
  17. }
  18. }
  19. }
  20. }
  21. )
  22. puts response

Js

  1. const response = await client.search({
  2. index: "ip_addresses",
  3. size: 0,
  4. aggs: {
  5. ip_ranges: {
  6. ip_range: {
  7. field: "ip",
  8. ranges: [
  9. {
  10. mask: "10.0.0.0/25",
  11. },
  12. {
  13. mask: "10.0.0.127/25",
  14. },
  15. ],
  16. },
  17. },
  18. },
  19. });
  20. console.log(response);

コンソール

  1. GET /ip_addresses/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. { "mask": "10.0.0.0/25" },
  10. { "mask": "10.0.0.127/25" }
  11. ]
  12. }
  13. }
  14. }
  15. }

応答:

コンソール結果

  1. {
  2. ...
  3. "aggregations": {
  4. "ip_ranges": {
  5. "buckets": [
  6. {
  7. "key": "10.0.0.0/25",
  8. "from": "10.0.0.0",
  9. "to": "10.0.0.128",
  10. "doc_count": 128
  11. },
  12. {
  13. "key": "10.0.0.127/25",
  14. "from": "10.0.0.0",
  15. "to": "10.0.0.128",
  16. "doc_count": 128
  17. }
  18. ]
  19. }
  20. }
  21. }

キー付き応答

keyed フラグを true に設定すると、各バケットに一意の文字列キーが関連付けられ、範囲が配列ではなくハッシュとして返されます:

Python

  1. resp = client.search(
  2. index="ip_addresses",
  3. size=0,
  4. aggs={
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. {
  10. "to": "10.0.0.5"
  11. },
  12. {
  13. "from": "10.0.0.5"
  14. }
  15. ],
  16. "keyed": True
  17. }
  18. }
  19. },
  20. )
  21. print(resp)

Ruby

  1. response = client.search(
  2. index: 'ip_addresses',
  3. body: {
  4. size: 0,
  5. aggregations: {
  6. ip_ranges: {
  7. ip_range: {
  8. field: 'ip',
  9. ranges: [
  10. {
  11. to: '10.0.0.5'
  12. },
  13. {
  14. from: '10.0.0.5'
  15. }
  16. ],
  17. keyed: true
  18. }
  19. }
  20. }
  21. }
  22. )
  23. puts response

Js

  1. const response = await client.search({
  2. index: "ip_addresses",
  3. size: 0,
  4. aggs: {
  5. ip_ranges: {
  6. ip_range: {
  7. field: "ip",
  8. ranges: [
  9. {
  10. to: "10.0.0.5",
  11. },
  12. {
  13. from: "10.0.0.5",
  14. },
  15. ],
  16. keyed: true,
  17. },
  18. },
  19. },
  20. });
  21. console.log(response);

コンソール

  1. GET /ip_addresses/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. { "to": "10.0.0.5" },
  10. { "from": "10.0.0.5" }
  11. ],
  12. "keyed": true
  13. }
  14. }
  15. }
  16. }

応答:

コンソール結果

  1. {
  2. ...
  3. "aggregations": {
  4. "ip_ranges": {
  5. "buckets": {
  6. "*-10.0.0.5": {
  7. "to": "10.0.0.5",
  8. "doc_count": 10
  9. },
  10. "10.0.0.5-*": {
  11. "from": "10.0.0.5",
  12. "doc_count": 260
  13. }
  14. }
  15. }
  16. }
  17. }

各範囲のキーをカスタマイズすることも可能です:

Python

  1. resp = client.search(
  2. index="ip_addresses",
  3. size=0,
  4. aggs={
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. {
  10. "key": "infinity",
  11. "to": "10.0.0.5"
  12. },
  13. {
  14. "key": "and-beyond",
  15. "from": "10.0.0.5"
  16. }
  17. ],
  18. "keyed": True
  19. }
  20. }
  21. },
  22. )
  23. print(resp)

Ruby

  1. response = client.search(
  2. index: 'ip_addresses',
  3. body: {
  4. size: 0,
  5. aggregations: {
  6. ip_ranges: {
  7. ip_range: {
  8. field: 'ip',
  9. ranges: [
  10. {
  11. key: 'infinity',
  12. to: '10.0.0.5'
  13. },
  14. {
  15. key: 'and-beyond',
  16. from: '10.0.0.5'
  17. }
  18. ],
  19. keyed: true
  20. }
  21. }
  22. }
  23. }
  24. )
  25. puts response

Js

  1. const response = await client.search({
  2. index: "ip_addresses",
  3. size: 0,
  4. aggs: {
  5. ip_ranges: {
  6. ip_range: {
  7. field: "ip",
  8. ranges: [
  9. {
  10. key: "infinity",
  11. to: "10.0.0.5",
  12. },
  13. {
  14. key: "and-beyond",
  15. from: "10.0.0.5",
  16. },
  17. ],
  18. keyed: true,
  19. },
  20. },
  21. },
  22. });
  23. console.log(response);

コンソール

  1. GET /ip_addresses/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "ip_ranges": {
  6. "ip_range": {
  7. "field": "ip",
  8. "ranges": [
  9. { "key": "infinity", "to": "10.0.0.5" },
  10. { "key": "and-beyond", "from": "10.0.0.5" }
  11. ],
  12. "keyed": true
  13. }
  14. }
  15. }
  16. }

応答:

コンソール結果

  1. {
  2. ...
  3. "aggregations": {
  4. "ip_ranges": {
  5. "buckets": {
  6. "infinity": {
  7. "to": "10.0.0.5",
  8. "doc_count": 10
  9. },
  10. "and-beyond": {
  11. "from": "10.0.0.5",
  12. "doc_count": 260
  13. }
  14. }
  15. }
  16. }
  17. }