2014-09-18 45 views
2

好的,我的目标是对我的收藏进行文本搜索,然后过滤这些结果以确保它们落入我的“甜甜圈”几何图形中。从蒙戈网站例如:

enter image description here

这里的艰难的部分。蒙戈的文件证实,今天你不能合并$文本其中的美妙和$附近:

您可以在$附近的运营商,这需要特殊的地理空间索引,而不是结合与使用不同类型的查询运算符或命令特别指数。例如,您不能将$ 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当前限制的详细示例将是一个上帝派!多谢你们!

+0

您不能混合查询同时需要“地理空间”和“文本”索引。你可以做的唯一的事情就是与地理空间相结合的“$ regex”操作。但是他们不会在这里发生“排名”,所以对于如何解决结果还有点不清楚。 – 2014-09-19 01:29:41

+0

您可以在查询参数中使用[geonear aggregation](http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/)和正则表达式。这将返回按距离排序的结果。如果你想排除甜甜圈内圈的那些,你可以排除更近的那些。正如尼尔所说,你不能排列文本的东西,这将是一个全或无的正则表达式。 – 2014-09-19 13:23:07

回答

1

似乎有在我的情况是一对夫妇的解决方案:

  1. 使用蒙戈连接和整合类似的Solr或elasticsearch
  2. 使用蒙戈的$的方法来执行“嵌套查询”。这需要两个查询分贝
  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的队看的时候,我们将能够跨越多个索引进行搜索。希望这有帮助!