Cloudant / Bluemix 地理搜索

Cloudant / Bluemix geo search

我正在尝试使用来自 Bluemix 运行时的 nano 库查询 cloudant 数据库。 目标是将最近的 200 个地理点带回给定位置。我有一个用虚拟数据设置的云数据库和一个带有相关字段的搜索索引函数:

function(doc){
    index("status", doc.status, {"store":true, index:true});
    index("lat",doc.latitude,{"store": true, index:true});
    index("lon",doc.longitude,{"store": true, index:true});
}

我可以直接查询 cloudant 数据库... https://my_cloudant_details/databasename/_design/mainDesignDoc/_search/search_location?q=status:1&limit=200&sort=%22%3Cdistance,lon,lat,5.0,50.0,km%3E%22 这会返回一个包含 200 个位置的列表,这些位置按靠近中心 (5.0,50.0) 排序。数据库中填充了一组 20000 个虚拟点,上面的搜索带回了距离中心 3 到 60 公里的地方,所以一切看起来都不错。

我现在需要在 node.js bluemix 运行时中执行此操作。下面的函数被调用来对数据库进行搜索...

app.get('/search', function(request, response) {

    var latitude = request.query.latitude;
    var longitude = request.query.longitude;
    var qString = 'status:[1 TO 1]&limit=180&sort="<distance,lon,lat,' + longitude + ',' + latitude + ',km>"';

    database.search('mainDesignDoc', 'search_location', {q:qString}, function(err, body) {
        if (!err) {
            var dbdata = [];
            body.rows.forEach(function(doc) {
                dbdata.push(doc.fields);
            });
            response.send(dbdata);
        } else {
            response.send("error: " + err);
        }
    });
});

这returns25个结果,其中,点没有排序,也不是离中心最近的25个点。我已经为 qString 尝试了不同的语法来尝试让它工作,但没有找到任何有效的方法。

如能提供帮助,我们将不胜感激!!!

谢谢

代码似乎错误地将限制和排序参数指定为搜索查询 (q) 的一部分。以下应该生成正确的 URL,我认为:

app.get('/search', function(request, response) {

    var latitude = request.query.latitude;
    var longitude = request.query.longitude;
    var qString = 'status:[1 TO 1]';
    var sortString = '"<distance,lon,lat,' + longitude + ',' + latitude + ',km>"';
    database.search('mainDesignDoc', 'search_location', {q:qString, limit:180, sort:sortString}, function(err, body) {

        if (!err) {
            var dbdata = [];
            body.rows.forEach(function(doc) {
                dbdata.push(doc.fields);
            });
            response.send(dbdata);
        } else {
            response.send("error: " + err);
        }
    });
});