定義
geoShapeインデックス定義で
indexShapesがtrueに設定されている場合、geoShape演算子は特定のジオメトリに関連するシェイプのクエリをサポートします。検索する座標を指定する場合は、最初に 経度 、次に 緯度 を指定する必要があります。 経度の値は、両方を含む
-180と180の間で指定できます。 緯度の値は-90と90の間で指定できます。 座標値は整数または double にすることができます。注意
MongoDB Search は以下の機能をサポートしていません。
非デフォルトの座標参照システム(CRS)
平面 XY 座標系 (2 次元)
座標ペアのポイント表記(つまり
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 では、次の用語を使用してクエリを作成します。
フィールド | タイプ | 説明 | 必要性 |
|---|---|---|---|
| GeoJSON オブジェクト | 検索する 多角形 、 MultiPolygon 、または LineString の形状または点を指定する GeoJSON オブジェクト。多角形は最後の位置が最初の位置と同じである閉じたループとして指定する必要があります。 地理空間結果を計算する場合、 MongoDB Search geoShape 演算子と geoWithin 演算子、およびMongoDB $geoIntersects 演算子は異なるジオメトリを使用します。この違いは、 MongoDB Search とMongoDB が多角形エッジを描画する方法で確認できます。 MongoDB Search は、座標参照システム内の 2 点間の最小ラインである直列距離 に基づいて多角形を描画します。 MongoDB は、 ジオメトリ タイプ のサードパーティ ライブラリ上に構築された2 dsphere インデックス 、または 平面モードに基づきジオデックスモードを使用して多角形を読み込み、2 d インデックス から収集します。詳細については、 「GeoJSON オブジェクト」 を参照してください。 MongoDB Search とMongoDBでは、多角形に関係する地理空間クエリで異なる結果が返される可能性がありました。 | はい |
| 文字列または複数の文字列の配列 | 検索するインデックス付き geo 型フィールド。 | はい |
| 列挙 | クエリシェイプ ジオメトリとインデックス付きフィールド ジオメトリの関係。 値は次のいずれかになります。
| はい |
| オブジェクト | 一致する検索結果に割り当てる スコア 。 デフォルトでは、結果のスコアは
クエリで | 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 MongoDB Search index for geo type and run the example queries on your cluster. The MongoDB Search Quick Start contains instructions for loading the sample dataset, creating an index definition, and running MongoDB Search queries.
以下は、 listingsAndReviewsコレクションの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": "token" 15 } 16 } 17 } 18 }
等価一致の例
次の例では、 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 ])
{ "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演算子を使用して、スペインの指定された経度と緯度の座標で交差するプロパティを検索します。
クエリには以下が含まれます。
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 ])
{ "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フィールドを検索します。
クエリには以下が含まれます。
基本検索
次のクエリは、指定された検索条件に一致するドキュメントを返します。
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 ])
{ "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 }
メタデータ検索
次のクエリは、指定された検索条件に対するプロパティの種類(アパート、家など)の数を返します。
1 db.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') } ] } } } ]
クエリ結果は、指定された座標におけるさまざまな種類のプロパティを示します。