Mongo: 聚合 $geoNear 和 $text 没有结果
Mongo: aggregate $geoNear and $text no results
我正在尝试在 Mongoose 中执行 geoNear + 文本搜索聚合查询:
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
返回了正确的结果。我是否错误地链接了查询?
只有第一个 $match
阶段可以使用索引,因此您不能在第二个 $match
阶段使用文本索引。您也不能在同一 $match
中结合使用 2dsphere 索引和文本索引。一种选择是切换文本搜索 $match
阶段和 $geoNear
阶段的顺序。交换后,文本搜索将使用文本索引,如果您设置 spherical : false
,$geoNear
仍然有效。 $geoNear
将计算平面距离,而不是球面距离,并且不会使用索引。
如果那不可行,我们可以在您描述用例的情况下尝试考虑其他选择。
对于@wdberkeley 的回答,您可以使用 $geoWithin
而不是 $geoNear
阶段。
db.landmarkSchema.aggregate([
{$match: {
$text: {$search: "great test text"} ,
loc: {$geoWithin: {$centerSphere: [[ 14.3, 48.3], 5/6731]}}
}}])
注意:不会使用地理索引!
更多信息:http://docs.mongodb.org/manual/reference/operator/query/geoWithin/
我正在尝试在 Mongoose 中执行 geoNear + 文本搜索聚合查询:
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
返回了正确的结果。我是否错误地链接了查询?
只有第一个 $match
阶段可以使用索引,因此您不能在第二个 $match
阶段使用文本索引。您也不能在同一 $match
中结合使用 2dsphere 索引和文本索引。一种选择是切换文本搜索 $match
阶段和 $geoNear
阶段的顺序。交换后,文本搜索将使用文本索引,如果您设置 spherical : false
,$geoNear
仍然有效。 $geoNear
将计算平面距离,而不是球面距离,并且不会使用索引。
如果那不可行,我们可以在您描述用例的情况下尝试考虑其他选择。
对于@wdberkeley 的回答,您可以使用 $geoWithin
而不是 $geoNear
阶段。
db.landmarkSchema.aggregate([
{$match: {
$text: {$search: "great test text"} ,
loc: {$geoWithin: {$centerSphere: [[ 14.3, 48.3], 5/6731]}}
}}])
注意:不会使用地理索引!
更多信息:http://docs.mongodb.org/manual/reference/operator/query/geoWithin/