geoShape
定义
geoShape
如果在索引定义中将
indexShapes
设置为true
,则geoShape
运算符支持查询与给定几何图形相关的形状。指定要搜索的坐标时,必须先指定经度,然后指定纬度。 经度值可以介于
-180
和180
之间,两者均包括在内。 纬度值可以介于-90
和90
之间,两者均包括在内。 坐标值可以是整数或双精度值。注意
Atlas Search 不支持以下内容:
非默认坐标参考系 (CRS)
平面 XY 坐标系(二维)
坐标对 点表示法(即
pointFieldName: [12, 34]
)
语法
1 { 2 "$search": { 3 "index": <index name>, // optional, defaults to "default" 4 "geoShape": { 5 "path": "<field-to-search>", 6 "relation": "contains | disjoint | intersects | within", 7 "geometry": <GeoJSON-object>, 8 "score": <score-options> 9 } 10 } 11 }
选项
geoShape
使用以下词条来构造查询:
字段 | 类型 | 说明 | 必要性 |
---|---|---|---|
geometry | GeoJSON 对象 | GeoJSON对象,用于指定要进行 的 Polygon 、 MultiPolygon 或 LineString Atlas Search形状或点。必须将多边形指定为闭环,其中最后一个位置与第一个位置相同。 计算地理空间结果时,Atlas Search geoShape和geoWithin操作符以及 MongoDB $geoIntersects操作符使用不同的几何图形。 从 Atlas Search 和 MongoDB 绘制多边形边的方式可以看出这种差异。 Atlas Search 根据 笛卡尔距离 绘制多边形 ,这是坐标参考系中两点之间的最短直线。 MongoDB 使用适用于 测地线类型 的第三方库绘制多边形 使用测地线。要了解详情,请参阅GeoJSON 对象。 对于涉及多边形的地理空间查询,Atlas Search 和 MongoDB 可能会返回不同的结果。 | 是 |
path | 字符串或字符串数组 | 是 | |
relation | 枚举 | 查询结构几何图形与索引字段几何图形的关系。值可以是以下之一:
| 是 |
score | 对象 | 分配给匹配搜索结果的分数。 默认情况下,结果中的分数为
有关在查询中使用 | no |
示例
listingsAndReviews
sample_airbnb
以下示例使用数据库中的collection集合。如果集群上有样本数据集,则可以为地理类型创建自定义 Atlas Search 索引,并在集群上运行示例查询。
提示
如果您已经加载示例数据集,请按照 Atlas Search 入门教程,创建索引定义并运行 Atlas Search 查询。
以下是对listingsAndReviews
collection中的address.location
字段进行索引的索引定义示例:
1 { 2 "mappings": { 3 "fields": { 4 "address": { 5 "fields": { 6 "location": { 7 "indexShapes": true, 8 "type": "geo" 9 } 10 }, 11 "type": "document" 12 } 13 } 14 } 15 }
Atlas Search 入门包含有关加载样本数据集、创建索引定义和运行 Atlas Search 搜索查询的说明。
注意
对于以下示例查询,请确保索引定义中的indexShapes
设置为true
。
不相交示例
以下示例使用geoShape
操作符搜索与指定的夏威夷经度和纬度坐标没有共同之处的属性。
查询包括:
1 db.listingsAndReviews.aggregate([ 2 { 3 "$search": { 4 "geoShape": { 5 "relation": "disjoint", 6 "geometry": { 7 "type": "Polygon", 8 "coordinates": [[[-161.323242,22.512557], 9 [-152.446289,22.065278], 10 [-156.09375,17.811456], 11 [-161.323242,22.512557]]] 12 }, 13 "path": "address.location" 14 } 15 } 16 }, 17 { 18 $limit: 3 19 }, 20 { 21 $project: { 22 "_id": 0, 23 "name": 1, 24 "address": 1, 25 score: { $meta: "searchScore" } 26 } 27 } 28 ])
该查询返回以下搜索结果:
1 { 2 "name" : "Ribeira Charming Duplex", 3 "address" : { 4 "street" : "Porto, Porto, Portugal", 5 "suburb" : "", 6 "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória", 7 "market" : "Porto", 8 "country" : "Portugal", 9 "country_code" : "PT", 10 "location" : { 11 "type" : "Point", 12 "coordinates" : [ -8.61308, 41.1413 ], 13 "is_location_exact" : false 14 } 15 } 16 } 17 { 18 "name" : "Horto flat with small garden", 19 "address" : { 20 "street" : "Rio de Janeiro, Rio de Janeiro, Brazil", 21 "suburb" : "Jardim Botânico", 22 "government_area" : "Jardim Botânico", 23 "market" : "Rio De Janeiro", 24 "country" : "Brazil", 25 "country_code" : "BR", 26 "location" : { 27 "type" : "Point", 28 "coordinates" : [ -43.23074991429229, -22.966253551739655 ], 29 "is_location_exact" : true 30 } 31 } 32 } 33 { 34 "name" : "Private Room in Bushwick", 35 "address" : { 36 "street" : "Brooklyn, NY, United States", 37 "suburb" : "Brooklyn", 38 "government_area" : "Bushwick", 39 "market" : "New York", 40 "country" : "United States", 41 "country_code" : "US", 42 "location" : { 43 "type" : "Point", 44 "coordinates" : [ -73.93615, 40.69791 ], 45 "is_location_exact" : true 46 } 47 } 48 }
相交示例
以下示例使用geoShape
操作符搜索与西班牙的指定经度和纬度坐标相交的属性。
查询包括:
1 db.listingsAndReviews.aggregate([ 2 { 3 "$search": { 4 "geoShape": { 5 "relation": "intersects", 6 "geometry": { 7 "type": "MultiPolygon", 8 "coordinates": [ 9 [[[2.16942,41.40082], 10 [2.17963,41.40087], 11 [2.18146,41.39716], 12 [2.15533,41.40686], 13 [2.14596,41.38475], 14 [2.17519,41.41035], 15 [2.16942,41.40082]]], 16 [[[2.16365,41.39416], 17 [2.16963,41.39726], 18 [2.15395,41.38005], 19 [2.17935,41.43038], 20 [2.16365,41.39416]]] 21 ] 22 }, 23 "path": "address.location" 24 } 25 } 26 }, 27 { 28 $limit: 3 29 }, 30 { 31 $project: { 32 "_id": 0, 33 "name": 1, 34 "address": 1, 35 score: { $meta: "searchScore" } 36 } 37 } 38 ])
该查询返回以下搜索结果:
1 { 2 "name" : "Cozy bedroom Sagrada Familia", 3 "address" : { 4 "street" : "Barcelona, Catalunya, Spain", 5 "suburb" : "Eixample", 6 "government_area" : "el Fort Pienc", 7 "market" : "Barcelona", 8 "country" : "Spain", 9 "country_code" : "ES", 10 "location" : { 11 "type" : "Point", 12 "coordinates" : [ 2.17963, 41.40087 ], 13 "is_location_exact" : true 14 } 15 } 16 } 17 { 18 "name" : "", 19 "address" : { 20 "street" : "Barcelona, Catalunya, Spain", 21 "suburb" : "Vila de Gràcia", 22 "government_area" : "la Vila de Gràcia", 23 "market" : "Barcelona", 24 "country" : "Spain", 25 "country_code" : "ES", 26 "location" : { 27 "type" : "Point", 28 "coordinates" : [ 2.15759, 41.40349 ], 29 "is_location_exact" : true 30 } 31 } 32 } 33 { 34 "name" : "SPACIOUS RAMBLA CATALUÑA", 35 "address" : { 36 "street" : "Barcelona, Catalunya, Spain", 37 "suburb" : "L'Antiga Esquerra de l'Eixample", 38 "government_area" : "l'Antiga Esquerra de l'Eixample", 39 "market" : "Barcelona", 40 "country" : "Spain", 41 "country_code" : "ES", 42 "location" : { 43 "type" : "Point", 44 "coordinates" : [ 2.15255, 41.39193 ], 45 "is_location_exact" : true 46 } 47 } 48 }
示例内
以下示例使用geoShape
操作符搜索纽约州位于指定经度和纬度坐标内的房产。 这些查询搜索sample_airbnb
数据库的listingsAndReviews
集合中的address.location
字段。
查询包括:
1 db.listingsAndReviews.aggregate([ 2 { 3 "$search": { 4 "geoShape": { 5 "relation": "within", 6 "geometry": { 7 "type": "Polygon", 8 "coordinates": [[[-74.3994140625,40.5305017757], 9 [-74.7290039063,40.5805846641], 10 [-74.7729492188,40.9467136651], 11 [-74.0698242188,41.1290213475], 12 [-73.65234375,40.9964840144], 13 [-72.6416015625,40.9467136651], 14 [-72.3559570313,40.7971774152], 15 [-74.3994140625,40.5305017757]]] 16 }, 17 "path": "address.location" 18 } 19 } 20 }, 21 { 22 $limit: 3 23 }, 24 { 25 $project: { 26 "_id": 0, 27 "name": 1, 28 "address": 1, 29 score: { $meta: "searchScore" } 30 } 31 } 32 ])
该查询返回以下搜索结果:
1 { 2 "name" : "Private Room in Bushwick", 3 "address" : { 4 "street" : "Brooklyn, NY, United States", 5 "suburb" : "Brooklyn", 6 "government_area" : "Bushwick", 7 "market" : "New York", 8 "country" : "United States", 9 "country_code" : "US", 10 "location" : { 11 "type" : "Point", 12 "coordinates" : [ -73.93615, 40.69791 ], 13 "is_location_exact" : true 14 } 15 }, 16 { 17 "name" : "New York City - Upper West Side Apt", 18 "address" : { 19 "street" : "New York, NY, United States", 20 "suburb" : "Manhattan", 21 "government_area" : "Upper West Side", 22 "market" : "New York", 23 "country" : "United States", 24 "country_code" : "US", 25 "location" : { 26 "type" : "Point", 27 "coordinates" : [ -73.96523, 40.79962 ], 28 "is_location_exact" : false 29 } 30 }, 31 "score" : 1 32 } 33 { 34 "name" : "Deluxe Loft Suite", 35 "address" : { 36 "street" : "Brooklyn, NY, United States", 37 "suburb" : "Greenpoint", 38 "government_area" : "Greenpoint", 39 "market" : "New York", 40 "country" : "United States", 41 "country_code" : "US", 42 "location" : { 43 "type" : "Point", 44 "coordinates" : [ -73.94472, 40.72778 ], 45 "is_location_exact" : true 46 } 47 }, 48 "score" : 1 49 }