AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

near (MongoDB Search 演算子)

MongoDB Searchインデックスがある場合でも、near 演算子を使用して配列に保存されている数値または日付値をクエリすることはできません。範囲は、配列内のインデックス付き数値または日付値をクエリする目的でのみ使用できます。

near

near演算子は、数値、日付、GeoJSON ポイント値のクエリとスコアリングをサポートしています。 この演算子は、以下の検索を実行するために使用できます。

  • BSON int32 、 int64 、 doubleデータ型の数値フィールド。

  • BSON date型の日付フィールドは、:ISODate 形式です。

  • 緯度と経度の座標を使用して定義される地理的ロケーション フィールド。

near 演算子を使用すると、数値または日付に近い結果を見つけることができます。near 演算子は、数値または日付に近接することでMongoDB Search 結果をスコア付けします。

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 では、次の用語を使用してクエリを作成します。

フィールド
タイプ
説明
必要性

origin

日付、数値、または地理的

近くの検索する数値、日付、または地理的ポイント。 これは、結果の近接性が測定されるオリジンです。

  • 数値フィールドの場合、値は BSON int32 、 int64 、またはdoubleデータ型である必要があります。

  • 日付フィールドの場合、値は ISODate 形式の日付である必要があります。

はい

path

文字列または複数の文字列の配列

インデックス付きフィールドまたは検索するフィールド。

はい

pivot

数値

MongoDB Search 結果ドキュメントのスコアを計算するために使用する値。スコアは、次の式を使用して計算されます。

pivotscore = ------------------ pivot + distance

上記で、 distanceはoriginとインデックス付きフィールド値の差です。

結果のインデックス フィールド値がoriginからpivot単位離れている場合、スコアは1/2 (または0.5 )と等しくなります。 pivotの値は より大きくなければなりません( > ) 0 。

originが の場合:

  • 数値、 pivotは整数または浮動小数点数として指定できます。

  • 日付、 pivotはミリ秒単位で指定する必要があり、 32または64ビット整数として指定できます。 例:

    • 1 分は次と等しい: 60,000 ms

    • 1 時間は に等しい 3,600,000 ms

    • 1 日は に等しい 86,400,000 ms

    • 1 か月(または 30 日)は に等しい 2,592,000,000 ms

  • GeoJSON ポイントであるpivotはメートル単位で測定され、整数または浮動小数点数として指定する必要があります。

はい

score

オブジェクト

一致する検索結果に割り当てる スコア 。 デフォルトのスコアは、次のオプションを使用して変更できます。

  • boost: 結果のスコアに指定された数値を掛けます。

  • constant: 結果のスコアを指定された数値に置き換えます。

  • function: 結果のスコアを指定された式で置き換えます。

クエリで score を使用する方法については、「結果内のドキュメントのスコアリング」を参照してください。

詳しくは、「スコアリング動作 」を参照してください。

no

MongoDB Search score は、 MongoDB Search 結果の origin に対する近接性を測定します。score は 0 と 1 の間でスケーリングされ、1 は完全一致、0 は距離一致です。origin からのMongoDB Search の結果の距離が、pivot を使用して計算されたオリジンからの距離と等しい場合、スコアは 0.5 と等しくなります。

near演算子は、ドキュメントスコアを計算するために以下の距離減衰関数を使用します。

pivotscore = ------------------ pivot + distance

ここで、各期間は次のように定義されます。

係数
説明

pivot

fieldValueとoriginの距離がそれに等しい場合に、スコアを0.5と等しくするための参照点として指定される値。 これは、 fieldValueとoriginの距離が大きくなるにつれてスコアがどの程度早く低下するかを定義します。 fieldValueとoriginの間の特定の距離の場合、 pivotが減少すると、スコアも減少します。

distance

fieldValue と origin の間の絶対距離。MongoDB Search は、次の式を使用してこの値を計算します。

abs(fieldValue - origin)

以下の条件に一致するもの。

  • fieldValue これは、ドキュメント内でクエリしているフィールドの値です。

  • origin は、付近を検索する番号、日付、または地理的な点です。

クエリで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 のスコアを受け取ります。

次の例では、 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}

ドキュメンテーション