【问题标题】:mongodb $text with $nearmongodb $text 与 $near
【发布时间】: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


【解决方案1】:

在我的案例场景中似乎有几个解决方案:

  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 团队,以了解我们何时能够跨多个索引执行搜索。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2012-11-29
    相关资源
    最近更新 更多