2016-09-30 49 views
1

我正在研究搜索功能,用户可以在其中搜索某个餐馆。该场景是:基于带文本索引的经纬度信息的猫鼬排序文档

我有餐厅收藏,其中我有多个位置的经度和纬度信息。 现在正在搜索,

我有用户的位置信息(与经度和纬度)。因此,我必须根据用户位置从最近到最远排序餐厅,并且必须对使用文本索引索引的字段内容进行文本搜索。

我有不同的搜索场景,所以我使用聚合管道和投影进行结果评分。 有人可以建议我从哪里开始?

在此先感谢。

+0

的MongoDB使用geonear查询。 –

回答

0

尝试以下步骤:

第一步:

添加位置字段架构中的并在其上创建索引2dsphere。

location: { type: [Number], index: '2dsphere' } 

您还可以动态地创建索引:

db.collection.createIndex({ <location field> : "2dsphere" }) 

第二步

现在创建$ geoNear汇总查询:

var aggregateQuery = [{ 
     "$geoNear": { 
      "near": { 
       "type": "Point", 
       "coordinates": [options.longitude, options.latitude] 
      }, 
      "distanceField": 'dis', 
      "distanceMultiplier": .001, 
      "spherical": true, 
      "query": **conditions** 
     } 
    }]; 

你可以通过任何条件查询参数。

db.collectionname.aggregate(aggregateQuery).exec(function(err, result){ 
// use result here 
}) 

注:记住存储在位置阵列经度第一和然后纬度。

欲了解更多详情,请参阅此链接: https://docs.mongodb.com/manual/reference/command/geoNear/

希望这有助于!

UPDATE

db.collectionname.aggregate([ 
    {$match: { 
     $text: {$search: "text"} , 
     location: {$geoWithin: {$centerSphere: [[ longitude, latitude], points]}} 
    }}]) 

注:地理索引将不会被使用!

欲了解更多信息请参考以下链接http://docs.mongodb.org/manual/reference/operator/query/geoWithin/

+0

谢谢,但我不能使用这种方法。我正在使用具有$ textSearch的聚合管道。根据mongo doc,$ goNear应该是聚合管道中的第一项。 $ textSearch有相同的限制。我不能使用$ textSearch删除。有没有办法在聚合管道中使用这两种方法? –

+0

@varun请检查更新。 – Sachin