本文介绍了Mongo:总计$ geoNear和$ text无结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Mongoose中执行geoNear +文本搜索聚合查询:

I'm trying to do a geoNear + text search aggregate query in Mongoose:

landmarkSchema.aggregate(
   [
      { "$geoNear": {
        "near": {
          "type": "Point",
          "coordinates": [parseFloat(userCoord1), parseFloat(userCoord0)]
        },
        "distanceField": "distance",
        "minDistance": 1,
        "maxDistance": 5000,
        "spherical": true,
        "query": { "loc.type": "Point" }
      } },
      { $match: { $text: { $search: sText } } },
      { $sort: { score: { $meta: "textScore" } } }

  ],
  function(err,data) {
    if (data){
      res.send(data);
    }
    else {
        console.log('no results');
        res.send({err:'no results'});
    }
});

但是Mongo没有返回任何结果.当我分别执行每个查询时,返回$geoNear$match : $text正确的结果.我是否错误地链接了查询?

But Mongo is not returning any results. When I perform each query separately, $geoNear and $match : $text the correct results are returned. Am I chaining the query incorrectly?

推荐答案

只有初始的$match阶段可以使用索引,因此不能在第二个$match中使用文本索引.您也不能在同一$match中使用2dsphere索引和文本索引进行合并.一种选择是切换文本搜索$match阶段和$geoNear阶段的顺序.交换后,文本搜索将使用文本索引,并且如果您设置spherical : false$geoNear仍将起作用. $geoNear将计算平面距离,而不是球形距离,并且不使用索引.

Only an initial $match stage can use an index, so you cannot use a text index in the second $match. You also can't combine using a 2dsphere index and using a text index in the same $match. One option is switching the order of the text search $match stage and the $geoNear stage. Swapped, the text search will use a text index and $geoNear will still work if you set spherical : false. $geoNear will calculate planar, not spherical, distances, and will not use an index.

如果这不可行,那么当您描述用例时,我们可以尝试考虑其他选择.

If that's not feasible, we could try to think of other options if you describe the use case.

这篇关于Mongo:总计$ geoNear和$ text无结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:12