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搜索结果文档分数的值。分数使用以下公式计算: 其中, 当结果的索引字段值与 如果
| 是 |
| 对象 | 分配给匹配搜索结果的分数。您可以使用以下选项修改默认分数:
有关在查询中使用 要了解更多信息,请参阅评分行为。 | no |
评分行为
MongoDB Search score 是MongoDB Search 结果与 origin 的接近程度的度量。score 在 0 和 1 之间调整,其中 1 为精确匹配,0 为远程匹配。当MongoDB搜索结果与 origin 的距离等于使用 pivot 计算的距原点的距离时,分数等于 0.5。
near 操作符使用以下距离衰减函数来计算文档得分:
pivotscore = ------------------ pivot + distance其中每个术语的定义如下:
因子 | 说明 |
|---|---|
| 指定为参考点的值,如果 |
|
其中:
|
可以在查询中使用 score 选项修改默认分数。要了解有关选项的详细信息,请参阅修改分数。
示例
数字和日期示例使用sample_mflix数据库中的movies集合。 GeoJSON 点示例使用sample_airbnb数据库中的listingsAndReviews集合。
如果在集群上加载示例数据,则可以使用以下示例中的索引定义或动态索引创建静态索引,并在集群上运行示例查询。
提示
如果您已经加载了 示例数据集,请参阅 MongoDB Search 快速入门教程以创建 索引定义并运行 MongoDB Search 查询。
数字示例
以下示例使用 near 操作符查询数字字段。以下查询使用名为 runtimes 的索引,该索引动态地为 movies 集合中的所有字段创建索引。该查询在 movies 集合中搜索 runtime 字段值接近 279 的文档。
基本示例
以下查询将返回所有符合搜索条件的文档。它包括一个用于将输出限制为 7 个结果的$limit阶段和一个用于执行以下操作的$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搜索结果中,电影 The Kingdom 和 The Jinx: The Life and Deaths of Robert Durst 的分数为 1.0,因为它们的 runtime字段值 279 完全匹配。电影 Les Misèrables 和 Tokyo Trial 的分数为 0.5,因为它们的 runtime字段值与 279 相差 2 个单位。
元数据示例
以下查询返回搜索条件的元数据结果。也就是说,它使用 $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" } } }}以下查询搜索 9 月13 、 1前后上映的电影。 它包括一个用于将输出限制为3结果的$limit阶段和一个用于执行以下操作的$project阶段:
排除
title和released之外的所有字段添加字段
score
结果的 score 将使用 pivot 计算得出。
注意
pivot 此处以毫秒为单位,7,776,000,000 ms 等于大约三个月。
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 的分数约为 0.5,因为 1915-09-13 的 released字段值与 origin 的距离约为 7,776,000,000 毫秒。
GeoJSON 点示例
以下示例使用 near 操作符查询 sample_airbnb.listingsAndReviews 集合中的 GeoJSON 点对象。以下索引定义对 listingsAndReviews 集合中的 address.location 和 property_type 字段编制索引。
例子
{ "mappings": { "fields": { "address": { "fields": { "location": { "type": "geo" } }, "type": "document" }, "property_type": { "type": "string" } } }}基本示例
以下示例使用 near 操作符查询 sample_airbnb.listingsAndReviews 集合中的 address.location 字段。
例子
以下查询将搜索葡萄牙的属性。它包括一个 $limit 阶段,用于将输出限制为 3 个结果,以及一个 $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 操作符查询 sample_airbnb.listingsAndReviews 集合中的 property_type 和 address.location 字段。
例子
以下查询可搜索指定 GeoJSON point 附近的香港公寓。查询使用 must 来指定必须满足的搜索条件,并使用 should 指定对位置的偏好。它包括一个 $limit 阶段,用于将输出限制为 3 个结果,以及一个 $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}