用近$ mongodb $ text

好的,我的目标是对我的collections进行文本search,然后过滤这些结果以确保它们属于我的“甜甜圈”几何graphics。 来自mongo网站的例子:

在这里输入图像描述

这是艰难的部分。 Mongo的文档证实,今天你不能把$ text和$ near的精妙结合起来:

您不能将需要特殊地理空间索引的$ near操作符与使用不同types的特殊索引的查询操作符或命令组合在一起。 例如,您不能将$ 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目前的限制的详细的例子将是一个神派! 多谢你们!

似乎在我的情况下有几个解决scheme:

  1. 使用Mongo Connect并集成诸如solr或elasticsearch之类的东西
  2. 使用Mongo的$ in方法来执行“嵌套查询”。这需要对db进行两次查询
  3. 使用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团队联系,了解我们何时能够在多个索引中执行search。 希望这是有帮助的!