Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to limit or sort result of queryRadius function? #9

Open
ildar-icoosoft opened this issue Sep 24, 2017 · 11 comments
Open

How to limit or sort result of queryRadius function? #9

ildar-icoosoft opened this issue Sep 24, 2017 · 11 comments

Comments

@ildar-icoosoft
Copy link

Is it possible to limit or sort radius queries?
Here is my code:

let result = await myGeoTableManager.queryRadius({
    RadiusInMeter: radius,
    CenterPoint: {
        longitude: longitude,
        latitude: latitude,
    }
});
@robhogan
Copy link
Owner

robhogan commented Sep 25, 2017

You can use the QueryInput option for this, which is passed straight through to underlying DynamoDB call. See Amazon's docs for what the QueryInput object should look like.

Eg you might have something like (untested!):

let result = await myGeoTableManager.queryRadius({
    RadiusInMeter: radius,
    CenterPoint: {
        longitude: longitude,
        latitude: latitude,
    },
    QueryInput: {
        FilterExpression: "PlaceType = :type",
        ExpressionAttributeValues: { "type": { "S": "restaurant" } },
        Limit: 100,
        ...
    }
});

Note that in DynamoDB filtering is done after the data is retrieved but before it's sent to you, so this saves you a bit of bandwidth and computation but won't reduce the read units consumed.

@ildar-icoosoft
Copy link
Author

@rh389 It does not work

@hello-alf
Copy link

doesn't work

@tibeoh
Copy link

tibeoh commented Jan 22, 2018

If can help someone, it's just a little mistake in the @rh389 answer. It missing a semicolon prefix for :type key.

The correct example is:

let result = await myGeoTableManager.queryRadius({
    RadiusInMeter: radius,
    CenterPoint: {
        longitude: longitude,
        latitude: latitude,
    },
    QueryInput: {
        FilterExpression: "PlaceType = :type",
        ExpressionAttributeValues: { ":type": { "S": "restaurant" } },
        Limit: 100,
        ...
    }
});

I tried, and it works ;)

@ildar-icoosoft
Copy link
Author

@tibeoh filter expression works. Limit does not work

@lantikchi
Copy link

lantikchi commented Feb 28, 2018

Hi,
I am running into the same issue. I tried setting the limit for QueryInput but it doesn't work.

myGeoTableManager.queryRadius({
    RadiusInMeter: radius,
    CenterPoint: {
        longitude: fromLongitude,
        latitude: fromLatitude
    },
    QueryInput: {
        Limit: 50
    }
})
.then(result => {
        winston.info(`Read ${result.length} labels from ${req.config.TABLE_NAME}`);
        res.json(result);
    })   

is there another way to limit the number of results returned by queryRadius?

@a3leggeddog
Copy link

Hello -

Is there a way to order the results by distance to the lat/lng point originally passed to the queryRadius function? For example, finding all the restaurants within a radius sorted by distance to the user?

Thanks!

@robhogan
Copy link
Owner

robhogan commented Apr 1, 2018

@a3leggeddog There's no built-in way (PR would be welcome for this), but it'd be fairly straightforward to sort the results client-side. S2Utils has a getEarthDistance that will work out the distance between two lat/lon pairs.

Server-side isn't possible unfortunately because DynamoDB can't sort by a computed value.

To those asking about Limit, sorry I haven't had chance to work on this yet. I should've realised that it won't work as expected because each of our geo queries actually translates to multiple queries of DynamoDB, so passing through Limit will simply apply the limit to each sub-query and not to our overall result. That will bound things to some extent but it's still possible you could get around 6*limit items back. Again there's no easy way out of that - we'd just have to throw away some results after they're retrieved (which is similar to what DynamoDB does when limits are used alongside filter expressions).

@vazch
Copy link

vazch commented Apr 13, 2018

Hello @rh389

How can i work with ProjectionExpression?I try like ProjectionExpression: "nameAtrribute"
but it give me this error -> TypeError: Cannot read property 'S' of undefined

@ashish-nodejs
Copy link

my table : {
"name": "user1",
"latitude": 48.21,
"longitude": 16.37
},
{
"name": "user2",
"latitude": 53.9,
"longitude": 27.57
},
and i want to get user lies in radius of given lat long ... is any way to get this with AWS LAMBDA & Dynamodb

@martineisele
Copy link

Hi @rh389 ,

by any chance, did you have time to work on the functionality to limit the results? Since Limit is the base for pagination it would be great to have this feature in place. Or do you have an alternative in mind for pagination?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants