首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongodb $text与$near

mongodb $text与$near
EN

Stack Overflow用户
提问于 2014-09-18 21:50:17
回答 1查看 719关注 0票数 3

好的,我的目标是对我的集合执行文本搜索,然后过滤那些结果,以确保它们属于我的“甜甜圈”几何图形。mongo站点的示例:

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

不能将需要特殊地理空间索引的$near运算符与使用不同类型特殊索引的查询运算符或命令组合起来。例如,不能将$near与$text查询组合。

所以..。这是我今天要做的事。请注意,我目前的方法没有实现“甜甜圈”几何(它只是一个直径增长的圆,当用户在地图上“放大”时返回重复的结果)。

代码语言:javascript
复制
    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方法,但我正不由自主地提出这个查询。详细的例子,如何解决芒果目前的限制将是一个上帝-发送!谢谢你们!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-23 07:22:40

在我的案例中,似乎有几种解决方案:

  1. 使用Mongo连接并集成诸如solr或elasticsearch之类的东西
  2. 使用Mongo的$in方法执行“嵌套查询”。这需要对db进行两个查询。
  3. 使用Mongo的$regex方法(正如其他人所描述的,我在下面已经演示过)。 PostModel.find(坐标:{ $near:{ $geometry:{$geometry:{$geometry:"Point",坐标: userLong,userLat },$maxDistance: maxDistance,$minDistance: minDistance },$or: mongoCategories,术语:{ "$regex":术语,"$options":'i‘} },函数(err,结果){/系统错误。重定向用户,以便他们可以报告错误。如果(err) { console.log(err);回调(主体);//如果没有找到结果,则为:如果(!结果){回调(结果);}如果(结果){回调(结果);}

我已经联系了Mongodb团队,看看我们什么时候能够跨多个索引执行搜索。希望这能帮上忙!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25922965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档