好的,我的目标是对我的收藏执行文本搜索,然后过滤这些结果以确保它们在我的“ donut ”几何范围内。来自 mongo 站点的示例:
这是困难的部分。 Mongo 的文档证实,今天您无法将 $text 和 $near 的优点结合起来:
You cannot combine the $near operator, which requires a special geospatial index, with a query operator or command that uses a different type of special index. For example you cannot combine $near with the $text query.
Sooo.. 这就是我今天要做的事情。请注意,我当前的方法没有实现“ donut ”几何形状(它只是一个直径不断增大的圆,当用户在 map 上“缩小”时会返回重复的结果)。
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 电流限制的详细示例将是天赐之物!谢谢大家!
最佳答案
在我的案例场景中似乎有几个解决方案:
- 使用 Mongo Connect 并集成诸如 solr 或 elasticsearch 之类的东西
- 使用 Mongo 的 $in 方法执行“嵌套查询”.. 这需要对数据库进行两次查询
使用 Mongo 的 $regex 方法(正如其他人所描述的,我在下面进行了演示)。
PostModel.find({ coordinates: { $near: { $geometry: { type: "Point", coordinates: [ userLong , userLat ] }, $maxDistance: maxDistance, $minDistance: minDistance } }, $or: mongoCategories, term: { "$regex": term, "$options": 'i' } }, function (err, results) { //systematic error. Redirect user so they can report error. if (err) { console.log(err); callback(body); // if no result found } else if (!results) { callback(results); } else if (results) { callback(results); } });
我已经联系了 Mongodb 团队,了解我们何时能够跨多个索引执行搜索。希望这对您有所帮助!
关于node.js - mongodb $text 与 $near,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25922965/