near 演算子の制限
MongoDB Searchインデックスがある場合でも、near 演算子を使用して配列に保存されている数値または日付値をクエリすることはできません。範囲は、配列内のインデックス付き数値または日付値をクエリする目的でのみ使用できます。
定義
構文
near の構文は次のとおりです。
{ $search: { "index": <index name>, // optional, defaults to "default" "near": { "path": "<field-to-search>", "origin": <date-or-number>, "pivot": <pivot-distance>, "score": <score-options> } }}オプション
near では、次の用語を使用してクエリを作成します。
フィールド | タイプ | 説明 | 必要性 |
|---|---|---|---|
| 日付、数値、または地理的 | 近くの検索する数値、日付、または地理的ポイント。 これは、結果の近接性が測定されるオリジンです。
| はい |
| 文字列または複数の文字列の配列 | インデックス付きフィールドまたは検索するフィールド。 | はい |
| 数値 | MongoDB Search 結果ドキュメントのスコアを計算するために使用する値。スコアは、次の式を使用して計算されます。 上記で、 結果のインデックス フィールド値が
| はい |
| オブジェクト | 一致する検索結果に割り当てる スコア 。 デフォルトのスコアは、次のオプションを使用して変更できます。
クエリで 詳しくは、「スコアリング動作 」を参照してください。 | no |
スコアリングの動作
MongoDB Search score は、 MongoDB Search 結果の origin に対する近接性を測定します。score は 0 と 1 の間でスケーリングされ、1 は完全一致、0 は距離一致です。origin からのMongoDB Search の結果の距離が、pivot を使用して計算されたオリジンからの距離と等しい場合、スコアは 0.5 と等しくなります。
near演算子は、ドキュメントスコアを計算するために以下の距離減衰関数を使用します。
pivotscore = ------------------ pivot + distanceここで、各期間は次のように定義されます。
係数 | 説明 |
|---|---|
|
|
|
以下の条件に一致するもの。
|
クエリでscoreオプションを使用してデフォルトのスコアを変更できます。 オプションの詳細については、 「 スコアの変更 」を参照してください。
例
数値と日付の例では、 sample_mflixデータベース内のmoviesコレクションが使用されます。 GeoJSON ポイントの例では、 sample_airbnbデータベースのlistingsAndReviewsコレクションを使用します。
クラスターにサンプルデータをロードすると、以下の例のインデックス定義または動的インデックスを使用して静的インデックスを作成し、クラスターで例クエリを実行できます。
Tip
サンプル データセットをすでに読み込んでいる場合は、MongoDB Search クイック スタート チュートリアルを参照して、インデックス定義を作成し、MongoDB Search クエリを実行します。
数値の例
次の例では、near 演算子を使用して数値フィールドをクエリします。次のクエリは、movies コレクション内のすべてのフィールドに動的にインデックスを付ける runtimes という名前のインデックスを使用します。このクエリは、movies コレクション内で runtime フィールドの値が 279 に近いドキュメントを検索します。
基本的な例
次のクエリは、検索条件に一致するすべてのドキュメントを返します。出力を の結果に制限する$limit ステージと、次の操作を行う ステージが含まれています。7$project
titleとruntimeを除くすべてのフィールドを除外次のフィールドを追加:
score
scoreはpivotを使用して計算されます。
db.movies.aggregate([ { $search: { "index": "runtimes", "near": { "path": "year", "origin": 2000, "pivot": 2 } } }, { $limit: 7 }, { $project: { "_id": 0, "title": 1, "runtime": 1, score: { $meta: "searchScore" } } }])[ { runtime: 279, title: 'The Kingdom', score: 1 }, { runtime: 279, title: 'The Kingdom', score: 1 }, { runtime: 279, title: 'The Jinx: The Life and Deaths of Robert Durst', score: 1 }, { runtime: 281, title: 'Les Misèrables', score: 0.5 }, { runtime: 277, title: 'Tokyo Trial', score: 0.5 }, { runtime: 283, title: 'Scenes from a Marriage', score: 0.3333333432674408 }, { runtime: 274, title: 'The Crimson Petal and the White', score: 0.2857142984867096 }]MongoDB Search の結果では、映画 The Kingdom と The Jinx: The Life and Deaths of Robert Durst は、279 の runtimeフィールド値が完全一致であるため、1.0 のスコアを受け取ります。映画「Les Misèrables」と「Tokyo Trial」は、runtimeフィールド値が 279 から 2 単位であるため、スコアは 0.5 になります。
メタデータの例
次のクエリは、検索条件に一致するメタデータの結果を返します。このクエリでは $searchMeta ステージを使用して、次のバケット(年)内で検索条件に一致する映画数を取得します。
2000 (このバケットの下限値を含む)
2005: 2000 バケットの 排他的上限であり、このバケットの下限
2010: 2005 バケットの 排他的上限であり、このバケットの下限
2010: 2010 の排他的上限
db.movies.aggregate([ { "$searchMeta": { "facet": { "operator": { "near": { "path": "runtime", "origin": 279, "pivot": 2 } }, "facets": { "yearFacet": { "type": "number", "path": "year", "boundaries": [2000, 2005, 2010, 2015 ] } } } } }])[ { count: { lowerBound: Long('20910') }, facet: { yearFacet: { buckets: [ { _id: 2000, count: Long('3058') }, { _id: 2005, count: Long('4012') }, { _id: 2010, count: Long('4669') } ] } } }]日付の例
次の例では、 near演算子を使用して日付フィールドをクエリします。
例
releaseddateという名前の次のインデックス定義は、 moviesコレクション内のreleasedフィールド値をインデックス化します。
{ "mappings": { "dynamic": false, "fields": { "released": { "type": "date" } } }}次のクエリは、 13 、 1年 9 月 日頃に公開された映画を検索します。 出力を3の結果に制限する$limitステージと、次の操作を行う$projectステージが含まれています。
titleとreleasedを除くすべてのフィールドを除外次のフィールドを追加:
score
結果のscoreはpivotを使用して計算されます。
注意
pivot ここでは、 はミリ秒単位で測定され、 7,776,000,000 msは約 3 か月に等しくなります。
db.movies.aggregate([ { $search: { "index": "releaseddate", "near": { "path": "released", "origin": ISODate("1915-09-13T00:00:00.000+00:00"), "pivot": 7776000000 } } }, { $limit: 3 }, { $project: { "_id": 0, "title": 1, "released": 1, score: { $meta: "searchScore" } } }])上記のクエリは、次の検索結果を返します。
{ "title" : "Regeneration", "released" : ISODate("1915-09-13T00:00:00Z"), "score" : 1 }{ "title" : "The Cheat", "released" : ISODate("1915-12-13T00:00:00Z"), "score" : 0.49723756313323975 }{ "title" : "Hell's Hinges", "released" : ISODate("1916-03-05T00:00:00Z"), "score" : 0.34090909361839294 }上記のMongoDB Search 結果では、映画「Regeneration」には 1 のスコアが与えられます。これは、1915-09-13 の releasedフィールド値が完全一致であるためです。1915-12-13 に公開された映画 The Cheat は、origin からの releasedフィールド値の距離が 1915-09-13 から約 7,776,000,000 ミリ秒であるため、約 0.5 のスコアを受け取ります。
GeoJSON ポイントの例
次の例では、 near演算子を使用して、 sample_airbnb.listingsAndReviewsコレクション内の GeoJSON ポイント オブジェクトをクエリします。 address.locationproperty_type次のインデックス定義は、listingsAndReviews コレクション内の フィールドと フィールドにインデックスを作成します。
例
{ "mappings": { "fields": { "address": { "fields": { "location": { "type": "geo" } }, "type": "document" }, "property_type": { "type": "string" } } }}基本的な例
次の例では、 near演算子を使用して、 sample_airbnb.listingsAndReviewsコレクションのaddress.locationフィールドをクエリします。
例
次のクエリはポルトガルのプロパティを検索します。出力を 3 件の結果に制限する $limit ステージと、次のための $project ステージが含まれています。
nameとaddressを除くすべてのフィールドを除外次のフィールドを追加:
score
結果のscoreはpivotを使用して計算されます。 ここでは、 pivotはメートル単位で測定され、1000 メートルは 1 キロメートルに等しいことに注意してください。
db.listingsAndReviews.aggregate([ { "$search": { "near": { "origin": { "type": "Point", "coordinates": [-8.61308, 41.1413] }, "pivot": 1000, "path": "address.location" } } }, { $limit: 3 }, { $project: { "_id": 0, "name": 1, "address": 1, score: { $meta: "searchScore" } } }])上記のクエリは、次の検索結果を返します。
{ "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 } }, "score" : 1}{ "name" : "DB RIBEIRA - Grey Apartment", "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.61294, 41.14126 ], "is_location_exact" : true } }, "score" : 0.9876177310943604}{ "name" : "Ribeira 24 (4)", "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.61318, 41.14107 ], "is_location_exact" : false } }, "score" : 0.973789632320404}結果は、指定された座標から離れたプロパティはスコアが低いことを示しています。
複合例
compound次の例では、property_type address.location演算子を使用して、sample_airbnb.listingsAndReviews コレクション内の フィールドと フィールドをクエリします。
例
次のクエリは、指定された GeoJSON ポイントの近くの香港のアパートを検索します。クエリは、must を使用して満たされる必要がある検索条件を指定し、should を使用してロケーションの設定を指定します。出力を 3 件の結果に制限する $limit ステージと次のための $project ステージが含まれています。
property_typeとaddressを除くすべてのフィールドを除外次のフィールドを追加:
score
scoreはpivotフィールドを使用して計算されます。 ここでは、 pivotはメートル単位で測定され、1000 メートルは 1 キロメートルに等しいことに注意してください。
db.listingsAndReviews.aggregate([ { $search: { "compound": { "must": { "text": { "query": "Apartment", "path": "property_type" } }, "should": { "near": { "origin": { "type": "Point", "coordinates": [114.15027, 22.28158] }, "pivot": 1000, "path": "address.location" } } } } }, { $limit: 3 }, { $project: { "_id": 0, "property_type": 1, "address": 1, score: { $meta: "searchScore" } } }])上記のクエリは、次の検索結果を返します。
{ "property_type" : "Apartment", "address" : { "street" : "Hong Kong, Hong Kong Island, Hong Kong", "suburb" : "Central & Western District", "government_area" : "Central & Western", "market" : "Hong Kong", "country" : "Hong Kong", "country_code" : "HK", "location" : { "type" : "Point", "coordinates" : [ 114.15027, 22.28158 ], "is_location_exact" : true } }, "score" : 1.177286982536316}{ "property_type" : "Apartment", "address" : { "street" : "Hong Kong, Hong Kong Island, Hong Kong", "suburb" : "Central & Western District", "government_area" : "Central & Western", "market" : "Hong Kong", "country" : "Hong Kong", "country_code" : "HK", "location" : { "type" : "Point", "coordinates" : [ 114.15082, 22.28161 ], "is_location_exact" : true } }, "score" : 1.1236450672149658}{ "property_type" : "Apartment", "address" : { "street" : "Hong Kong, Hong Kong Island, Hong Kong", "suburb" : "Mid-Levels", "government_area" : "Central & Western", "market" : "Hong Kong", "country" : "Hong Kong", "country_code" : "HK", "location" : { "type" : "Point", "coordinates" : [ 114.15007, 22.28215 ], "is_location_exact" : true } }, "score" : 1.114811897277832}