Docs 菜单

geoShape

geoShape

如果在索引定义中将indexShapes设置为true ,则geoShape操作符支持查询与给定几何图形有关系的形状。

指定要搜索的坐标时,必须先指定经度,然后指定纬度。 经度值可以介于-180180之间,两者均包括在内。 纬度值可以介于-9090之间,两者均包括在内。 坐标值可以是整数或双精度值。

注意

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使用基于2 dsphere 索引的测地线模式绘制多边形,该索引构建在测地线类型第三方库之上,或者使用来自2 d 索引的平面模式绘制多边形。要学习;了解详情,请参阅GeoJSON对象。

对于涉及多边形的地理空间查询,Atlas Search 和 MongoDB 可能会返回不同的结果。

path

字符串或字符串数组

Indexed geo type field or fields to search.

relation

枚举

查询结构几何图形与索引字段几何图形的关系。值可以是以下之一:

  • contains - 表示索引的几何图形包含查询的几何图形。

  • disjoint - 表示查询几何图形和索引几何图形没有任何共同点。

  • intersects — 指示查询几何图形和索引几何图形相交。

  • within - 表示索引几何图形位于查询几何图形中。 不能将withinLineStringPoint一起使用。

score

对象

分配给匹配搜索结果的分数。 默认情况下,结果中的分数1 。 您可以使用以下选项修改分数:

  • boost:将生成的分数乘以给定数字。

  • constant:将结果分数替换为给定数字。

  • function:使用的给定表达式替换结果分数。

有关在查询中使用 score 的信息,请参阅对结果中的文档进行评分

no

The following examples use the listingsAndReviews collection in the sample_airbnb database. If you have the sample dataset on your cluster, you can create a custom Atlas Search index for geo type and run the example queries on your cluster. The Atlas Search Quick Start contains instructions for loading the sample dataset, creating an index definition, and running Atlas Search queries.

以下是对listingsAndReviewscollection中的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 "property_type": {
14 "type": "stringFacet"
15 }
16 }
17 }
18}

以下示例使用geoShape操作符搜索与指定的夏威夷经度和纬度坐标没有共同之处的属性。

查询包括:

  • $limit 阶段将输出限制为 3 个结果。

  • $project 阶段排除 nameaddress 以外的所有字段。

1db.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])
{
"name" : "Ribeira Charming Duplex",
"address" : {
"street" : "Porto, Porto, Portugal",
"suburb" : "",
"government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
"market" : "Porto",
"country" : "Portugal",
"country_code" : "PT",
"location" : {
"type" : "Point",
"coordinates" : [ -8.61308, 41.1413 ],
"is_location_exact" : false
}
}
}
{
"name" : "Horto flat with small garden",
"address" : {
"street" : "Rio de Janeiro, Rio de Janeiro, Brazil",
"suburb" : "Jardim Botânico",
"government_area" : "Jardim Botânico",
"market" : "Rio De Janeiro",
"country" : "Brazil",
"country_code" : "BR",
"location" : {
"type" : "Point",
"coordinates" : [ -43.23074991429229, -22.966253551739655 ],
"is_location_exact" : true
}
}
}
{
"name" : "Private Room in Bushwick",
"address" : {
"street" : "Brooklyn, NY, United States",
"suburb" : "Brooklyn",
"government_area" : "Bushwick",
"market" : "New York",
"country" : "United States",
"country_code" : "US",
"location" : {
"type" : "Point",
"coordinates" : [ -73.93615, 40.69791 ],
"is_location_exact" : true
}
}
}

以下示例使用geoShape操作符搜索与西班牙的指定经度和纬度坐标相交的属性。

查询包括:

  • $limit 阶段将输出限制为 3 个结果。

  • $project 阶段排除 nameaddress 以外的所有字段。

1db.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])
{
"name" : "Cozy bedroom Sagrada Familia",
"address" : {
"street" : "Barcelona, Catalunya, Spain",
"suburb" : "Eixample",
"government_area" : "el Fort Pienc",
"market" : "Barcelona",
"country" : "Spain",
"country_code" : "ES",
"location" : {
"type" : "Point",
"coordinates" : [ 2.17963, 41.40087 ],
"is_location_exact" : true
}
}
}
{
"name" : "",
"address" : {
"street" : "Barcelona, Catalunya, Spain",
"suburb" : "Vila de Gràcia",
"government_area" : "la Vila de Gràcia",
"market" : "Barcelona",
"country" : "Spain",
"country_code" : "ES",
"location" : {
"type" : "Point",
"coordinates" : [ 2.15759, 41.40349 ],
"is_location_exact" : true
}
}
}
{
"name" : "SPACIOUS RAMBLA CATALUÑA",
"address" : {
"street" : "Barcelona, Catalunya, Spain",
"suburb" : "L'Antiga Esquerra de l'Eixample",
"government_area" : "l'Antiga Esquerra de l'Eixample",
"market" : "Barcelona",
"country" : "Spain",
"country_code" : "ES",
"location" : {
"type" : "Point",
"coordinates" : [ 2.15255, 41.39193 ],
"is_location_exact" : true
}
}
}

以下示例使用geoShape操作符搜索纽约州位于指定经度和纬度坐标内的房产。 这些查询搜索sample_airbnb数据库的listingsAndReviews集合中的address.location字段。

查询包括:

  • $limit 阶段将输出限制为 3 个结果。

  • $project 阶段排除 nameaddress 以外的所有字段。

The following query returns the documents that match the specified search criteria.

1db.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])
{
"name" : "Private Room in Bushwick",
"address" : {
"street" : "Brooklyn, NY, United States",
"suburb" : "Brooklyn",
"government_area" : "Bushwick",
"market" : "New York",
"country" : "United States",
"country_code" : "US",
"location" : {
"type" : "Point",
"coordinates" : [ -73.93615, 40.69791 ],
"is_location_exact" : true
}
},
{
"name" : "New York City - Upper West Side Apt",
"address" : {
"street" : "New York, NY, United States",
"suburb" : "Manhattan",
"government_area" : "Upper West Side",
"market" : "New York",
"country" : "United States",
"country_code" : "US",
"location" : {
"type" : "Point",
"coordinates" : [ -73.96523, 40.79962 ],
"is_location_exact" : false
}
},
"score" : 1
}
{
"name" : "Deluxe Loft Suite",
"address" : {
"street" : "Brooklyn, NY, United States",
"suburb" : "Greenpoint",
"government_area" : "Greenpoint",
"market" : "New York",
"country" : "United States",
"country_code" : "US",
"location" : {
"type" : "Point",
"coordinates" : [ -73.94472, 40.72778 ],
"is_location_exact" : true
}
},
"score" : 1
}

The following query returns the number of types of properties (such as apartment, house, and so on) for the specified search criteria.

1db.listingsAndReviews.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "geoShape": {
7 "relation": "within",
8 "geometry": {
9 "type": "Polygon",
10 "coordinates": [[[-74.3994140625,40.5305017757],
11 [-74.7290039063,40.5805846641],
12 [-74.7729492188,40.9467136651],
13 [-74.0698242188,41.1290213475],
14 [-73.65234375,40.9964840144],
15 [-72.6416015625,40.9467136651],
16 [-72.3559570313,40.7971774152],
17 [-74.3994140625,40.5305017757]]]
18 },
19 "path": "address.location"
20 }
21 },
22 "facets": {
23 "propertyTypeFacet": {
24 "type": "string",
25 "path": "property_type"
26 }
27 }
28 }
29 }
30 }
31])
[
{
count: { lowerBound: Long('599') },
facet: {
propertyTypeFacet: {
buckets: [
{ _id: 'Apartment', count: Long('486') },
{ _id: 'House', count: Long('43') },
{ _id: 'Townhouse', count: Long('24') },
{ _id: 'Condominium', count: Long('19') },
{ _id: 'Loft', count: Long('19') },
{ _id: 'Guest suite', count: Long('2') },
{ _id: 'Guesthouse', count: Long('2') },
{ _id: 'Aparthotel', count: Long('1') },
{ _id: 'Hostel', count: Long('1') },
{ _id: 'Serviced apartment', count: Long('1') }
]
}
}
}
]

The query results show the different types of properties in the specified coordinates.