我有以下查询
db.hotels.aggregate([
{
$search: {
index:'txtIdx', // this is the index name
text: {
query:"sun",
path:['name','address.landmark','address.city','address.state','address.country'],
fuzzy: {
maxEdits:2,
prefixLength: 1,
},
},
},
},
{
$project: {
_id: 1,
name: 1,
address:1,
score: { $meta: "searchScore" }
}
},
{$limit:100},
])
在酒店的集合中还有一个叫做'location'的字段,它的坐标如下
"location": {
"type": "Point",
"coordinates": [
72.867804,
19.076033
]
}
如何使用 geonear 和这个搜索查询只返回用户附近的酒店,并提供纬度、经度和距离。
我也试过这个查询
{
$search: {
index:'searchIndex',
compound: {
must: {
text: {
query:'sun',
path:['name','address.landmark','address.city','address.state','address.country'],
fuzzy: {
maxEdits:2,
prefixLength: 3,
},
},
},
should: {
near:{
origin: {
type: 'Point',
coordinates: [-122.45665489904827,37.75118012951178],
},
pivot: 1000,
path: 'location'
},
}
}
}
},
但上面的查询返回的结果甚至不在那个位置附近。它返回的结果与“搜索”在没有“附近”的情况下提供的结果相同。我已经为位置创建了“地理”索引,但它仍然不返回附近的酒店。
或者除了使用 geonear 和搜索之外还有其他方法吗?我从过去 2 天开始就在尝试,但我没有发现任何有用的东西。我也只想使用模糊文本搜索。请让我知道是否有办法解决这个问题?