地理网格查询
编辑地理网格查询编辑
匹配与 GeoGrid 聚合中的网格单元相交的 geo_point
和 geo_shape
值。
该查询旨在通过提供桶的键来匹配位于地理网格聚合桶内的文档。对于 geohash 和 geotile 网格,该查询可用于 geo_point 和 geo_shape 字段。对于 geo_hex 网格,它只能用于 geo_point 字段。
示例编辑
假设已索引以下文档
response = client.indices.create( index: 'my_locations', body: { mappings: { properties: { location: { type: 'geo_point' } } } } ) puts response response = client.index( index: 'my_locations', id: 1, refresh: true, body: { location: 'POINT(4.912350 52.374081)', city: 'Amsterdam', name: 'NEMO Science Museum' } ) puts response response = client.index( index: 'my_locations', id: 2, refresh: true, body: { location: 'POINT(4.405200 51.222900)', city: 'Antwerp', name: 'Letterenhuis' } ) puts response response = client.index( index: 'my_locations', id: 3, refresh: true, body: { location: 'POINT(2.336389 48.861111)', city: 'Paris', name: 'Musée du Louvre' } ) puts response
PUT /my_locations { "mappings": { "properties": { "location": { "type": "geo_point" } } } } PUT /my_locations/_doc/1?refresh { "location" : "POINT(4.912350 52.374081)", "city": "Amsterdam", "name": "NEMO Science Museum" } PUT /my_locations/_doc/2?refresh { "location" : "POINT(4.405200 51.222900)", "city": "Antwerp", "name": "Letterenhuis" } PUT /my_locations/_doc/3?refresh { "location" : "POINT(2.336389 48.861111)", "city": "Paris", "name": "Musée du Louvre" }
geohash 网格编辑
使用 geohash_grid 聚合,可以根据其 geohash 值对文档进行分组
response = client.search( index: 'my_locations', body: { size: 0, aggregations: { grouped: { geohash_grid: { field: 'location', precision: 2 } } } } ) puts response
GET /my_locations/_search { "size" : 0, "aggs" : { "grouped" : { "geohash_grid" : { "field" : "location", "precision" : 2 } } } }
{ "took" : 10, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "grouped" : { "buckets" : [ { "key" : "u1", "doc_count" : 2 }, { "key" : "u0", "doc_count" : 1 } ] } } }
我们可以通过使用以下语法执行地理网格查询,使用桶键提取其中一个桶上的文档
GET /my_locations/_search { "query": { "geo_grid" :{ "location" : { "geohash" : "u0" } } } }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_locations", "_id" : "3", "_score" : 1.0, "_source" : { "location" : "POINT(2.336389 48.861111)", "city" : "Paris", "name" : "Musée du Louvre" } } ] } }
geotile 网格编辑
使用 geotile_grid 聚合,可以根据其 geotile 值对文档进行分组
response = client.search( index: 'my_locations', body: { size: 0, aggregations: { grouped: { geotile_grid: { field: 'location', precision: 6 } } } } ) puts response
GET /my_locations/_search { "size" : 0, "aggs" : { "grouped" : { "geotile_grid" : { "field" : "location", "precision" : 6 } } } }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "grouped" : { "buckets" : [ { "key" : "6/32/21", "doc_count" : 2 }, { "key" : "6/32/22", "doc_count" : 1 } ] } } }
我们可以通过使用以下语法执行地理网格查询,使用桶键提取其中一个桶上的文档
GET /my_locations/_search { "query": { "geo_grid" :{ "location" : { "geotile" : "6/32/22" } } } }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_locations", "_id" : "3", "_score" : 1.0, "_source" : { "location" : "POINT(2.336389 48.861111)", "city" : "Paris", "name" : "Musée du Louvre" } } ] } }
geohex 网格编辑
使用 geohex_grid 聚合,可以根据其 geohex 值对文档进行分组
GET /my_locations/_search { "size" : 0, "aggs" : { "grouped" : { "geohex_grid" : { "field" : "location", "precision" : 1 } } } }
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "grouped" : { "buckets" : [ { "key" : "81197ffffffffff", "doc_count" : 2 }, { "key" : "811fbffffffffff", "doc_count" : 1 } ] } } }
我们可以通过使用以下语法执行地理网格查询,使用桶键提取其中一个桶上的文档
GET /my_locations/_search { "query": { "geo_grid" :{ "location" : { "geohex" : "811fbffffffffff" } } } }
{ "took" : 26, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my_locations", "_id" : "3", "_score" : 1.0, "_source" : { "location" : "POINT(2.336389 48.861111)", "city" : "Paris", "name" : "Musée du Louvre" } } ] } }