ジオグリッドクエリ

geo_pointgeo_shape の値がジオグリッド集約からのグリッドセルと交差するものを一致させます。

このクエリは、バケットのキーを提供することによって、ジオグリッド集約のバケット内にあるドキュメントを一致させるように設計されています。ジオハッシュおよびジオタイルグリッドの場合、このクエリは geo_point および geo_shape フィールドに使用できます。ジオヘックスグリッドの場合、geo_point フィールドにのみ使用できます。

次のドキュメントがインデックスされていると仮定します:

Python

  1. resp = client.indices.create(
  2. index="my_locations",
  3. mappings={
  4. "properties": {
  5. "location": {
  6. "type": "geo_point"
  7. }
  8. }
  9. },
  10. )
  11. print(resp)
  12. resp1 = client.index(
  13. index="my_locations",
  14. id="1",
  15. refresh=True,
  16. document={
  17. "location": "POINT(4.912350 52.374081)",
  18. "city": "Amsterdam",
  19. "name": "NEMO Science Museum"
  20. },
  21. )
  22. print(resp1)
  23. resp2 = client.index(
  24. index="my_locations",
  25. id="2",
  26. refresh=True,
  27. document={
  28. "location": "POINT(4.405200 51.222900)",
  29. "city": "Antwerp",
  30. "name": "Letterenhuis"
  31. },
  32. )
  33. print(resp2)
  34. resp3 = client.index(
  35. index="my_locations",
  36. id="3",
  37. refresh=True,
  38. document={
  39. "location": "POINT(2.336389 48.861111)",
  40. "city": "Paris",
  41. "name": "Musée du Louvre"
  42. },
  43. )
  44. print(resp3)

Ruby

  1. response = client.indices.create(
  2. index: 'my_locations',
  3. body: {
  4. mappings: {
  5. properties: {
  6. location: {
  7. type: 'geo_point'
  8. }
  9. }
  10. }
  11. }
  12. )
  13. puts response
  14. response = client.index(
  15. index: 'my_locations',
  16. id: 1,
  17. refresh: true,
  18. body: {
  19. location: 'POINT(4.912350 52.374081)',
  20. city: 'Amsterdam',
  21. name: 'NEMO Science Museum'
  22. }
  23. )
  24. puts response
  25. response = client.index(
  26. index: 'my_locations',
  27. id: 2,
  28. refresh: true,
  29. body: {
  30. location: 'POINT(4.405200 51.222900)',
  31. city: 'Antwerp',
  32. name: 'Letterenhuis'
  33. }
  34. )
  35. puts response
  36. response = client.index(
  37. index: 'my_locations',
  38. id: 3,
  39. refresh: true,
  40. body: {
  41. location: 'POINT(2.336389 48.861111)',
  42. city: 'Paris',
  43. name: 'Musée du Louvre'
  44. }
  45. )
  46. puts response

Js

  1. const response = await client.indices.create({
  2. index: "my_locations",
  3. mappings: {
  4. properties: {
  5. location: {
  6. type: "geo_point",
  7. },
  8. },
  9. },
  10. });
  11. console.log(response);
  12. const response1 = await client.index({
  13. index: "my_locations",
  14. id: 1,
  15. refresh: "true",
  16. document: {
  17. location: "POINT(4.912350 52.374081)",
  18. city: "Amsterdam",
  19. name: "NEMO Science Museum",
  20. },
  21. });
  22. console.log(response1);
  23. const response2 = await client.index({
  24. index: "my_locations",
  25. id: 2,
  26. refresh: "true",
  27. document: {
  28. location: "POINT(4.405200 51.222900)",
  29. city: "Antwerp",
  30. name: "Letterenhuis",
  31. },
  32. });
  33. console.log(response2);
  34. const response3 = await client.index({
  35. index: "my_locations",
  36. id: 3,
  37. refresh: "true",
  38. document: {
  39. location: "POINT(2.336389 48.861111)",
  40. city: "Paris",
  41. name: "Musée du Louvre",
  42. },
  43. });
  44. console.log(response3);

コンソール

  1. PUT /my_locations
  2. {
  3. "mappings": {
  4. "properties": {
  5. "location": {
  6. "type": "geo_point"
  7. }
  8. }
  9. }
  10. }
  11. PUT /my_locations/_doc/1?refresh
  12. {
  13. "location" : "POINT(4.912350 52.374081)",
  14. "city": "Amsterdam",
  15. "name": "NEMO Science Museum"
  16. }
  17. PUT /my_locations/_doc/2?refresh
  18. {
  19. "location" : "POINT(4.405200 51.222900)",
  20. "city": "Antwerp",
  21. "name": "Letterenhuis"
  22. }
  23. PUT /my_locations/_doc/3?refresh
  24. {
  25. "location" : "POINT(2.336389 48.861111)",
  26. "city": "Paris",
  27. "name": "Musée du Louvre"
  28. }

ジオハッシュグリッド

ジオハッシュグリッド集約を使用すると、ドキュメントをそのジオハッシュ値に基づいてグループ化することができます:

Python

  1. resp = client.search(
  2. index="my_locations",
  3. size=0,
  4. aggs={
  5. "grouped": {
  6. "geohash_grid": {
  7. "field": "location",
  8. "precision": 2
  9. }
  10. }
  11. },
  12. )
  13. print(resp)

Ruby

  1. response = client.search(
  2. index: 'my_locations',
  3. body: {
  4. size: 0,
  5. aggregations: {
  6. grouped: {
  7. geohash_grid: {
  8. field: 'location',
  9. precision: 2
  10. }
  11. }
  12. }
  13. }
  14. )
  15. puts response

Js

  1. const response = await client.search({
  2. index: "my_locations",
  3. size: 0,
  4. aggs: {
  5. grouped: {
  6. geohash_grid: {
  7. field: "location",
  8. precision: 2,
  9. },
  10. },
  11. },
  12. });
  13. console.log(response);

コンソール

  1. GET /my_locations/_search
  2. {
  3. "size" : 0,
  4. "aggs" : {
  5. "grouped" : {
  6. "geohash_grid" : {
  7. "field" : "location",
  8. "precision" : 2
  9. }
  10. }
  11. }
  12. }

コンソール結果

  1. {
  2. "took" : 10,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "grouped" : {
  20. "buckets" : [
  21. {
  22. "key" : "u1",
  23. "doc_count" : 2
  24. },
  25. {
  26. "key" : "u0",
  27. "doc_count" : 1
  28. }
  29. ]
  30. }
  31. }
  32. }

これらのバケットの1つからドキュメントを抽出するには、次の構文を使用してバケットキーを使用してジオグリッドクエリを実行します:

Python

  1. resp = client.search(
  2. index="my_locations",
  3. query={
  4. "geo_grid": {
  5. "location": {
  6. "geohash": "u0"
  7. }
  8. }
  9. },
  10. )
  11. print(resp)

Js

  1. const response = await client.search({
  2. index: "my_locations",
  3. query: {
  4. geo_grid: {
  5. location: {
  6. geohash: "u0",
  7. },
  8. },
  9. },
  10. });
  11. console.log(response);

コンソール

  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "geo_grid" :{
  5. "location" : {
  6. "geohash" : "u0"
  7. }
  8. }
  9. }
  10. }

コンソール結果

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "my_locations",
  19. "_id" : "3",
  20. "_score" : 1.0,
  21. "_source" : {
  22. "location" : "POINT(2.336389 48.861111)",
  23. "city" : "Paris",
  24. "name" : "Musée du Louvre"
  25. }
  26. }
  27. ]
  28. }
  29. }

ジオタイルグリッド

ジオタイルグリッド集約を使用すると、ドキュメントをそのジオタイル値に基づいてグループ化することができます:

Python

  1. resp = client.search(
  2. index="my_locations",
  3. size=0,
  4. aggs={
  5. "grouped": {
  6. "geotile_grid": {
  7. "field": "location",
  8. "precision": 6
  9. }
  10. }
  11. },
  12. )
  13. print(resp)

Ruby

  1. response = client.search(
  2. index: 'my_locations',
  3. body: {
  4. size: 0,
  5. aggregations: {
  6. grouped: {
  7. geotile_grid: {
  8. field: 'location',
  9. precision: 6
  10. }
  11. }
  12. }
  13. }
  14. )
  15. puts response

Js

  1. const response = await client.search({
  2. index: "my_locations",
  3. size: 0,
  4. aggs: {
  5. grouped: {
  6. geotile_grid: {
  7. field: "location",
  8. precision: 6,
  9. },
  10. },
  11. },
  12. });
  13. console.log(response);

コンソール

  1. GET /my_locations/_search
  2. {
  3. "size" : 0,
  4. "aggs" : {
  5. "grouped" : {
  6. "geotile_grid" : {
  7. "field" : "location",
  8. "precision" : 6
  9. }
  10. }
  11. }
  12. }

コンソール結果

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "grouped" : {
  20. "buckets" : [
  21. {
  22. "key" : "6/32/21",
  23. "doc_count" : 2
  24. },
  25. {
  26. "key" : "6/32/22",
  27. "doc_count" : 1
  28. }
  29. ]
  30. }
  31. }
  32. }

これらのバケットの1つからドキュメントを抽出するには、次の構文を使用してバケットキーを使用してジオグリッドクエリを実行します:

Python

  1. resp = client.search(
  2. index="my_locations",
  3. query={
  4. "geo_grid": {
  5. "location": {
  6. "geotile": "6/32/22"
  7. }
  8. }
  9. },
  10. )
  11. print(resp)

Js

  1. const response = await client.search({
  2. index: "my_locations",
  3. query: {
  4. geo_grid: {
  5. location: {
  6. geotile: "6/32/22",
  7. },
  8. },
  9. },
  10. });
  11. console.log(response);

コンソール

  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "geo_grid" :{
  5. "location" : {
  6. "geotile" : "6/32/22"
  7. }
  8. }
  9. }
  10. }

コンソール結果

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "my_locations",
  19. "_id" : "3",
  20. "_score" : 1.0,
  21. "_source" : {
  22. "location" : "POINT(2.336389 48.861111)",
  23. "city" : "Paris",
  24. "name" : "Musée du Louvre"
  25. }
  26. }
  27. ]
  28. }
  29. }

ジオヘックスグリッド

ジオヘックスグリッド集約を使用すると、ドキュメントをそのジオヘックス値に基づいてグループ化することができます:

Python

  1. resp = client.search(
  2. index="my_locations",
  3. size=0,
  4. aggs={
  5. "grouped": {
  6. "geohex_grid": {
  7. "field": "location",
  8. "precision": 1
  9. }
  10. }
  11. },
  12. )
  13. print(resp)

Js

  1. const response = await client.search({
  2. index: "my_locations",
  3. size: 0,
  4. aggs: {
  5. grouped: {
  6. geohex_grid: {
  7. field: "location",
  8. precision: 1,
  9. },
  10. },
  11. },
  12. });
  13. console.log(response);

コンソール

  1. GET /my_locations/_search
  2. {
  3. "size" : 0,
  4. "aggs" : {
  5. "grouped" : {
  6. "geohex_grid" : {
  7. "field" : "location",
  8. "precision" : 1
  9. }
  10. }
  11. }
  12. }

コンソール結果

  1. {
  2. "took" : 2,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "aggregations" : {
  19. "grouped" : {
  20. "buckets" : [
  21. {
  22. "key" : "81197ffffffffff",
  23. "doc_count" : 2
  24. },
  25. {
  26. "key" : "811fbffffffffff",
  27. "doc_count" : 1
  28. }
  29. ]
  30. }
  31. }
  32. }

これらのバケットの1つからドキュメントを抽出するには、次の構文を使用してバケットキーを使用してジオグリッドクエリを実行します:

Python

  1. resp = client.search(
  2. index="my_locations",
  3. query={
  4. "geo_grid": {
  5. "location": {
  6. "geohex": "811fbffffffffff"
  7. }
  8. }
  9. },
  10. )
  11. print(resp)

Js

  1. const response = await client.search({
  2. index: "my_locations",
  3. query: {
  4. geo_grid: {
  5. location: {
  6. geohex: "811fbffffffffff",
  7. },
  8. },
  9. },
  10. });
  11. console.log(response);

コンソール

  1. GET /my_locations/_search
  2. {
  3. "query": {
  4. "geo_grid" :{
  5. "location" : {
  6. "geohex" : "811fbffffffffff"
  7. }
  8. }
  9. }
  10. }

コンソール結果

  1. {
  2. "took" : 26,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "my_locations",
  19. "_id" : "3",
  20. "_score" : 1.0,
  21. "_source" : {
  22. "location" : "POINT(2.336389 48.861111)",
  23. "city" : "Paris",
  24. "name" : "Musée du Louvre"
  25. }
  26. }
  27. ]
  28. }
  29. }