Reputation: 35
I'm trying to query a cloudant database using the nano library from a Bluemix runtime. The goal is to bring back the closest 200 geopoints to a given location. I have a cloudant database set up with dummy data and a search index function with the relevant fields:
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});
}
I can query the cloudant database directly with... 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 which brings back a list of 200 places ordered by proximity to the centre (5.0,50.0). The db is populated with a set of 20000 dummy points, and the above search brings back places between 3 and 60 Km away from centre, so all looks good.
I now need to do this in the node.js bluemix runtime. The below function gets called to do a search on the database...
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);
}
});
});
This returns 25 results, and within them, the points are not sorted, and are also not the closest 25 points to the centre. I've tried different syntaxes for qString to try to get it working, but not found anything that works.
Any help getting this working would be appreciated!!!
Thanks
Upvotes: 2
Views: 383
Reputation: 1765
It looks like the code is incorrectly specifying the limit and sort parameters as part of the search query (q). The following should generate the correct URL, I think:
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);
}
});
});
Upvotes: 2