【发布时间】:2014-09-23 15:22:40
【问题描述】:
好的,我的目标是对我的集合执行文本搜索,然后过滤这些结果以确保它们属于我的“甜甜圈”几何图形。来自 mongo 网站的示例:
这是最困难的部分。 Mongo 的文档证实,今天你无法将 $text 和 $near 的美妙结合起来:
您不能将需要特殊地理空间索引的 $near 运算符与使用不同类型特殊索引的查询运算符或命令结合使用。例如,您不能将 $near 与 $text 查询结合使用。
Sooo.. 这就是我今天要做的。请注意,我当前的方法没有实现“甜甜圈”几何(它只是一个直径增大的圆,当用户在地图上“缩小”时返回重复的结果)。
var vendorResponse = JSON.parse(body);
var vendorPosts = vendorResponse.postings;
//Get the location of the user
var userLat = req.query.lat;
var userLong = req.query.long;
//Get the item that furthest away from the user
var furthestitem = sortedVendorPosts[sortedVendorPosts.length - 1];
//Calculate the radius of the area
var radius = Math.sqrt( Math.pow((userLat-furthestitem.location.lat), 2) + Math.pow((userLong-furthestitem.location.long), 2) )
PostModel.find({
$text: { $search: heading, $language: 'english' },
$or: mongoCategories,
coordinates :
{ $geoWithin :
{
$centerSphere : [ [ userLong, userLat ] , radius ]
}
}
} , { score: {
$meta: "textScore"
}
}, function (err, results) {
});
我曾尝试使用 mongos $geoIntersects 方法,但我正在努力制定这个查询。如何解决 mongos 当前限制的详细示例将是天赐良机!谢谢大家!
【问题讨论】:
-
您不能将查询与同时要求“地理空间”和“文本”索引混合。您唯一能做的就是将
$regex操作与地理空间相结合。但是他们不会在这里发生“排名”,所以有点不清楚如何解决这个结果。 -
您可以在查询参数中使用带有正则表达式的geonear aggregation。这将返回按距离排序的结果。如果要排除甜甜圈内环中的那些,则可以排除较近的那些。正如 Neil 所说,您无法对事物的文本方面进行排名,这将是一个全有或全无的正则表达式。
标签: node.js mongodb geospatial