ジオバウンズ集約

すべての値を含む地理的バウンディングボックスを計算するメトリック集約で、GeopointまたはGeoshapeフィールドに対して行われます。

例:

Python

  1. resp = client.indices.create(
  2. index="museums",
  3. mappings={
  4. "properties": {
  5. "location": {
  6. "type": "geo_point"
  7. }
  8. }
  9. },
  10. )
  11. print(resp)
  12. resp1 = client.bulk(
  13. index="museums",
  14. refresh=True,
  15. operations=[
  16. {
  17. "index": {
  18. "_id": 1
  19. }
  20. },
  21. {
  22. "location": "POINT (4.912350 52.374081)",
  23. "name": "NEMO Science Museum"
  24. },
  25. {
  26. "index": {
  27. "_id": 2
  28. }
  29. },
  30. {
  31. "location": "POINT (4.901618 52.369219)",
  32. "name": "Museum Het Rembrandthuis"
  33. },
  34. {
  35. "index": {
  36. "_id": 3
  37. }
  38. },
  39. {
  40. "location": "POINT (4.914722 52.371667)",
  41. "name": "Nederlands Scheepvaartmuseum"
  42. },
  43. {
  44. "index": {
  45. "_id": 4
  46. }
  47. },
  48. {
  49. "location": "POINT (4.405200 51.222900)",
  50. "name": "Letterenhuis"
  51. },
  52. {
  53. "index": {
  54. "_id": 5
  55. }
  56. },
  57. {
  58. "location": "POINT (2.336389 48.861111)",
  59. "name": "Musée du Louvre"
  60. },
  61. {
  62. "index": {
  63. "_id": 6
  64. }
  65. },
  66. {
  67. "location": "POINT (2.327000 48.860000)",
  68. "name": "Musée d'Orsay"
  69. }
  70. ],
  71. )
  72. print(resp1)
  73. resp2 = client.search(
  74. index="museums",
  75. size="0",
  76. query={
  77. "match": {
  78. "name": "musée"
  79. }
  80. },
  81. aggs={
  82. "viewport": {
  83. "geo_bounds": {
  84. "field": "location",
  85. "wrap_longitude": True
  86. }
  87. }
  88. },
  89. )
  90. print(resp2)

Ruby

  1. response = client.indices.create(
  2. index: 'museums',
  3. body: {
  4. mappings: {
  5. properties: {
  6. location: {
  7. type: 'geo_point'
  8. }
  9. }
  10. }
  11. }
  12. )
  13. puts response
  14. response = client.bulk(
  15. index: 'museums',
  16. refresh: true,
  17. body: [
  18. {
  19. index: {
  20. _id: 1
  21. }
  22. },
  23. {
  24. location: 'POINT (4.912350 52.374081)',
  25. name: 'NEMO Science Museum'
  26. },
  27. {
  28. index: {
  29. _id: 2
  30. }
  31. },
  32. {
  33. location: 'POINT (4.901618 52.369219)',
  34. name: 'Museum Het Rembrandthuis'
  35. },
  36. {
  37. index: {
  38. _id: 3
  39. }
  40. },
  41. {
  42. location: 'POINT (4.914722 52.371667)',
  43. name: 'Nederlands Scheepvaartmuseum'
  44. },
  45. {
  46. index: {
  47. _id: 4
  48. }
  49. },
  50. {
  51. location: 'POINT (4.405200 51.222900)',
  52. name: 'Letterenhuis'
  53. },
  54. {
  55. index: {
  56. _id: 5
  57. }
  58. },
  59. {
  60. location: 'POINT (2.336389 48.861111)',
  61. name: 'Musée du Louvre'
  62. },
  63. {
  64. index: {
  65. _id: 6
  66. }
  67. },
  68. {
  69. location: 'POINT (2.327000 48.860000)',
  70. name: "Musée d'Orsay"
  71. }
  72. ]
  73. )
  74. puts response
  75. response = client.search(
  76. index: 'museums',
  77. size: 0,
  78. body: {
  79. query: {
  80. match: {
  81. name: 'musée'
  82. }
  83. },
  84. aggregations: {
  85. viewport: {
  86. geo_bounds: {
  87. field: 'location',
  88. wrap_longitude: true
  89. }
  90. }
  91. }
  92. }
  93. )
  94. puts response

Js

  1. const response = await client.indices.create({
  2. index: "museums",
  3. mappings: {
  4. properties: {
  5. location: {
  6. type: "geo_point",
  7. },
  8. },
  9. },
  10. });
  11. console.log(response);
  12. const response1 = await client.bulk({
  13. index: "museums",
  14. refresh: "true",
  15. operations: [
  16. {
  17. index: {
  18. _id: 1,
  19. },
  20. },
  21. {
  22. location: "POINT (4.912350 52.374081)",
  23. name: "NEMO Science Museum",
  24. },
  25. {
  26. index: {
  27. _id: 2,
  28. },
  29. },
  30. {
  31. location: "POINT (4.901618 52.369219)",
  32. name: "Museum Het Rembrandthuis",
  33. },
  34. {
  35. index: {
  36. _id: 3,
  37. },
  38. },
  39. {
  40. location: "POINT (4.914722 52.371667)",
  41. name: "Nederlands Scheepvaartmuseum",
  42. },
  43. {
  44. index: {
  45. _id: 4,
  46. },
  47. },
  48. {
  49. location: "POINT (4.405200 51.222900)",
  50. name: "Letterenhuis",
  51. },
  52. {
  53. index: {
  54. _id: 5,
  55. },
  56. },
  57. {
  58. location: "POINT (2.336389 48.861111)",
  59. name: "Musée du Louvre",
  60. },
  61. {
  62. index: {
  63. _id: 6,
  64. },
  65. },
  66. {
  67. location: "POINT (2.327000 48.860000)",
  68. name: "Musée d'Orsay",
  69. },
  70. ],
  71. });
  72. console.log(response1);
  73. const response2 = await client.search({
  74. index: "museums",
  75. size: 0,
  76. query: {
  77. match: {
  78. name: "musée",
  79. },
  80. },
  81. aggs: {
  82. viewport: {
  83. geo_bounds: {
  84. field: "location",
  85. wrap_longitude: true,
  86. },
  87. },
  88. },
  89. });
  90. console.log(response2);

コンソール

  1. PUT /museums
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_point"
  7. }
  8. }
  9. }
  10. }
  11. POST /museums/_bulk?refresh
  12. {"index":{"_id":1}}
  13. {"location": "POINT (4.912350 52.374081)", "name": "NEMO Science Museum"}
  14. {"index":{"_id":2}}
  15. {"location": "POINT (4.901618 52.369219)", "name": "Museum Het Rembrandthuis"}
  16. {"index":{"_id":3}}
  17. {"location": "POINT (4.914722 52.371667)", "name": "Nederlands Scheepvaartmuseum"}
  18. {"index":{"_id":4}}
  19. {"location": "POINT (4.405200 51.222900)", "name": "Letterenhuis"}
  20. {"index":{"_id":5}}
  21. {"location": "POINT (2.336389 48.861111)", "name": "Musée du Louvre"}
  22. {"index":{"_id":6}}
  23. {"location": "POINT (2.327000 48.860000)", "name": "Musée d'Orsay"}
  24. POST /museums/_search?size=0
  25. {
  26. "query": {
  27. "match": { "name": "musée" }
  28. },
  29. "aggs": {
  30. "viewport": {
  31. "geo_bounds": {
  32. "field": "location",
  33. "wrap_longitude": true
  34. }
  35. }
  36. }
  37. }
geo_bounds集約は、境界を取得するために使用するフィールドを指定します。これはGeopointまたはGeoshapeタイプでなければなりません。
wrap_longitudeはオプションのパラメータで、バウンディングボックスが国際日付変更線を越えることを許可するかどうかを指定します。デフォルト値はtrueです。

上記の集約は、名前が「musée」と一致するすべてのドキュメントの位置フィールドのバウンディングボックスを計算する方法を示しています。

上記の集約に対する応答:

コンソール-結果

  1. {
  2. ...
  3. "aggregations": {
  4. "viewport": {
  5. "bounds": {
  6. "top_left": {
  7. "lat": 48.86111099738628,
  8. "lon": 2.3269999679178
  9. },
  10. "bottom_right": {
  11. "lat": 48.85999997612089,
  12. "lon": 2.3363889567553997
  13. }
  14. }
  15. }
  16. }
  17. }

geo_shapeフィールドにおけるジオバウンズ集約

ジオバウンズ集約は、geo_shapeフィールドでもサポートされています。

もしwrap_longitudetrue(デフォルト)に設定されている場合、バウンディングボックスは国際日付変更線を越えることができ、top_left経度がtop_right経度よりも大きい境界を返します。

例えば、地理的バウンディングボックスの右上の経度は通常、左下の経度よりも大きくなります。しかし、領域が180°子午線を越えると、左下の経度の値は右上の経度の値よりも大きくなります。詳細については、Open Geospatial Consortiumのウェブサイトの地理的バウンディングボックスを参照してください。

例:

Python

  1. resp = client.indices.create(
  2. index="places",
  3. mappings={
  4. "properties": {
  5. "geometry": {
  6. "type": "geo_shape"
  7. }
  8. }
  9. },
  10. )
  11. print(resp)
  12. resp1 = client.bulk(
  13. index="places",
  14. refresh=True,
  15. operations=[
  16. {
  17. "index": {
  18. "_id": 1
  19. }
  20. },
  21. {
  22. "name": "NEMO Science Museum",
  23. "geometry": "POINT(4.912350 52.374081)"
  24. },
  25. {
  26. "index": {
  27. "_id": 2
  28. }
  29. },
  30. {
  31. "name": "Sportpark De Weeren",
  32. "geometry": {
  33. "type": "Polygon",
  34. "coordinates": [
  35. [
  36. [
  37. 4.965305328369141,
  38. 52.39347642069457
  39. ],
  40. [
  41. 4.966979026794433,
  42. 52.391721758934835
  43. ],
  44. [
  45. 4.969425201416015,
  46. 52.39238958618537
  47. ],
  48. [
  49. 4.967944622039794,
  50. 52.39420969150824
  51. ],
  52. [
  53. 4.965305328369141,
  54. 52.39347642069457
  55. ]
  56. ]
  57. ]
  58. }
  59. }
  60. ],
  61. )
  62. print(resp1)
  63. resp2 = client.search(
  64. index="places",
  65. size="0",
  66. aggs={
  67. "viewport": {
  68. "geo_bounds": {
  69. "field": "geometry"
  70. }
  71. }
  72. },
  73. )
  74. print(resp2)

Ruby

  1. response = client.indices.create(
  2. index: 'places',
  3. body: {
  4. mappings: {
  5. properties: {
  6. geometry: {
  7. type: 'geo_shape'
  8. }
  9. }
  10. }
  11. }
  12. )
  13. puts response
  14. response = client.bulk(
  15. index: 'places',
  16. refresh: true,
  17. body: [
  18. {
  19. index: {
  20. _id: 1
  21. }
  22. },
  23. {
  24. name: 'NEMO Science Museum',
  25. geometry: 'POINT(4.912350 52.374081)'
  26. },
  27. {
  28. index: {
  29. _id: 2
  30. }
  31. },
  32. {
  33. name: 'Sportpark De Weeren',
  34. geometry: {
  35. type: 'Polygon',
  36. coordinates: [
  37. [
  38. [
  39. 4.965305328369141,
  40. 52.39347642069457
  41. ],
  42. [
  43. 4.966979026794433,
  44. 52.391721758934835
  45. ],
  46. [
  47. 4.969425201416015,
  48. 52.39238958618537
  49. ],
  50. [
  51. 4.967944622039794,
  52. 52.39420969150824
  53. ],
  54. [
  55. 4.965305328369141,
  56. 52.39347642069457
  57. ]
  58. ]
  59. ]
  60. }
  61. }
  62. ]
  63. )
  64. puts response
  65. response = client.search(
  66. index: 'places',
  67. size: 0,
  68. body: {
  69. aggregations: {
  70. viewport: {
  71. geo_bounds: {
  72. field: 'geometry'
  73. }
  74. }
  75. }
  76. }
  77. )
  78. puts response

Js

  1. const response = await client.indices.create({
  2. index: "places",
  3. mappings: {
  4. properties: {
  5. geometry: {
  6. type: "geo_shape",
  7. },
  8. },
  9. },
  10. });
  11. console.log(response);
  12. const response1 = await client.bulk({
  13. index: "places",
  14. refresh: "true",
  15. operations: [
  16. {
  17. index: {
  18. _id: 1,
  19. },
  20. },
  21. {
  22. name: "NEMO Science Museum",
  23. geometry: "POINT(4.912350 52.374081)",
  24. },
  25. {
  26. index: {
  27. _id: 2,
  28. },
  29. },
  30. {
  31. name: "Sportpark De Weeren",
  32. geometry: {
  33. type: "Polygon",
  34. coordinates: [
  35. [
  36. [4.965305328369141, 52.39347642069457],
  37. [4.966979026794433, 52.391721758934835],
  38. [4.969425201416015, 52.39238958618537],
  39. [4.967944622039794, 52.39420969150824],
  40. [4.965305328369141, 52.39347642069457],
  41. ],
  42. ],
  43. },
  44. },
  45. ],
  46. });
  47. console.log(response1);
  48. const response2 = await client.search({
  49. index: "places",
  50. size: 0,
  51. aggs: {
  52. viewport: {
  53. geo_bounds: {
  54. field: "geometry",
  55. },
  56. },
  57. },
  58. });
  59. console.log(response2);

コンソール

  1. PUT /places
  2. {
  3. "mappings": {
  4. "properties": {
  5. "geometry": {
  6. "type": "geo_shape"
  7. }
  8. }
  9. }
  10. }
  11. POST /places/_bulk?refresh
  12. {"index":{"_id":1}}
  13. {"name": "NEMO Science Museum", "geometry": "POINT(4.912350 52.374081)" }
  14. {"index":{"_id":2}}
  15. {"name": "Sportpark De Weeren", "geometry": { "type": "Polygon", "coordinates": [ [ [ 4.965305328369141, 52.39347642069457 ], [ 4.966979026794433, 52.391721758934835 ], [ 4.969425201416015, 52.39238958618537 ], [ 4.967944622039794, 52.39420969150824 ], [ 4.965305328369141, 52.39347642069457 ] ] ] } }
  16. POST /places/_search?size=0
  17. {
  18. "aggs": {
  19. "viewport": {
  20. "geo_bounds": {
  21. "field": "geometry"
  22. }
  23. }
  24. }
  25. }

コンソール-結果

  1. {
  2. ...
  3. "aggregations": {
  4. "viewport": {
  5. "bounds": {
  6. "top_left": {
  7. "lat": 52.39420966710895,
  8. "lon": 4.912349972873926
  9. },
  10. "bottom_right": {
  11. "lat": 52.374080987647176,
  12. "lon": 4.969425117596984
  13. }
  14. }
  15. }
  16. }
  17. }