2016-09-30 45 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