2014-09-18 52 views
2

好的,我的目標是對我的收藏進行文本搜索,然後過濾這些結果以確保它們落入我的「甜甜圈」幾何圖形中。從蒙戈網站例如:

enter image description here

這裏的艱難的部分。蒙戈的文件證實,今天你不能合併$文本其中的美妙和$附近:

您可以在$附近的運營商,這需要特殊的地理空間索引,而不是結合與使用不同類型的查詢運算符或命令特別指數。例如,您不能將$ near與$ text查詢結合使用。

Sooo ..這是我今天要做的。注意我目前的方法沒有達到「甜甜圈」幾何體(它只是一個直徑增長的圓圈,當用戶在地圖上「縮小」時會返回重複的結果)。

var vendorResponse = JSON.parse(body); 
    var vendorPosts = vendorResponse.postings; 

    //Get the location of the user 
    var userLat = req.query.lat; 
    var userLong = req.query.long; 

    //Get the item that furthest away from the user 
    var furthestitem = sortedVendorPosts[sortedVendorPosts.length - 1]; 

    //Calculate the radius of the area 
    var radius = Math.sqrt(Math.pow((userLat-furthestitem.location.lat), 2) + Math.pow((userLong-furthestitem.location.long), 2)) 

    PostModel.find({ 
     $text: { $search: heading, $language: 'english' }, 
     $or: mongoCategories, 
     coordinates : 
      { $geoWithin : 
       { 
        $centerSphere : [ [ userLong, userLat ] , radius ] 
       } 
      } 
     } , { score: { 
       $meta: "textScore" 
      } 
     }, function (err, results) { 




     }); 

我已經嘗試使用mongos $ geoIntersects方法,但我敲我的頭撞在牆上制定此查詢。如何解決mongos當前限制的詳細示例將是一個上帝派!多謝你們!

+0

您不能混合查詢同時需要「地理空間」和「文本」索引。你可以做的唯一的事情就是與地理空間相結合的「$ regex」操作。但是他們不會在這裏發生「排名」,所以對於如何解決結果還有點不清楚。 – 2014-09-19 01:29:41

+0

您可以在查詢參數中使用[geonear aggregation](http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/)和正則表達式。這將返回按距離排序的結果。如果你想排除甜甜圈內圈的那些,你可以排除更近的那些。正如尼爾所說,你不能排列文本的東西,這將是一個全或無的正則表達式。 – 2014-09-19 13:23:07

回答

1

似乎有在我的情況是一對夫婦的解決方案:

  1. 使用蒙戈連接和整合類似的Solr或elasticsearch
  2. 使用蒙戈的$的方法來執行「嵌套查詢」。這需要兩個查詢分貝
  3. 使用Mongo的$ regex方法(如其他人所述,我已在下面演示)。

    PostModel.find({ 
        coordinates: { $near: { 
         $geometry: { type: "Point", coordinates: [ userLong , userLat ] }, 
         $maxDistance: maxDistance, 
         $minDistance: minDistance 
         } 
        }, 
        $or: mongoCategories, 
        term: { "$regex": term, "$options": 'i' } 
    }, function (err, results) { 
        //systematic error. Redirect user so they can report error. 
        if (err) { 
         console.log(err); 
         callback(body); 
        // if no result found 
        } else if (!results) { 
         callback(results); 
        } else if (results) { 
         callback(results); 
        } 
    }); 
    

我伸手MongoDB的隊看的時候,我們將能夠跨越多個索引進行搜索。希望這有幫助!