Nearby API: Add sort parameter
Hi,
This is a follow up of https://github.com/pelias/pelias/issues/857.
In order to move step by step, I thought that the first one could be to add a sort parameter to the nearby API.
Possible values:
-
distance(default): records are sorted by distance, from the center point. -
popularity: records are sorted by popularity (desc). Popular venues within the area (given the focus point and radius) are placed on top.
Motivation: I want to be able to get popular venues within a given area.
Works well when used with the category filter: e.g: best bars around me.
Example:
/v1/nearby?point.lat=41.40140682071436&point.lon=2.191686630249024&debug=1&categories=nightlife&sort=popularity
Result:
0) Razzmatazz, Barcelona, Spain
1) L'Aliança del Poblenou, Barcelona, Spain
2) BeGood Club, Barcelona, Spain
3) Bodega Carol, Barcelona, Spain
4) El Cafè Blau, Barcelona, Spain
5) El Maderal, Barcelona, Spain
6) Atrium, Barcelona, Spain
7) Isami, Barcelona, Spain
8) Nicasso, Barcelona, Spain
9) La Tagliatella, Barcelona, Spain
Debug Query:
{
"query": {
"bool": {
"filter": [
{
"geo_distance": {
"distance": "1km",
"distance_type": "plane",
"center_point": {
"lat": 41.40140682071436,
"lon": 2.191686630249024
}
}
},
{
"terms": {
"layer": [
"venue",
"address",
"street"
]
}
},
{
"terms": {
"category": [
"nightlife"
]
}
}
]
}
},
"size": 20,
"track_scores": true,
"sort": [
"_score",
{
"popularity": {
"order": "desc"
}
}
]
}
I will appreciate your feedback.
Related PR: https://github.com/pelias/query/pull/123
TODO:
- [x] Add unit tests
Hi @bboure, To be honest, the nearby endpoint isn't currently something we spend a lot of time thinking about, so I doubt either @missinglink or I have strong feelings here.
Fortunately, this is open source, where the project maintainers don't have to have strong feelings about every single feature :)
So I'm happy to essentially make you the project manager of the nearby endpoint. I do think it makes sense to have a sort parameter that looks at popularity, so feel free to go forward with this feature. We'll merge improvements that you think are useful if they don't break other endpoints :rofl:
I've just invited you to the Pelias Contributors team which means you can create branches and open pull requests directly from the Pelias org, to make things easier on you. It also means our CI will generate docker images for any PRs you open.
:rocket:
Thank you for the invite @orangejulius I'll keep working on this then and let you known when I think this is mergeable.
Hi,
Some feedback on this. I have been testing this over the past few months. It hasn't made it to production (We haven't launched the product yet) but seems to be working pretty well, especially if the radius is limited to s small area and the layers are limited to venues only, which should be the recommended way of using this.
Example of usage: Top touristic venues in a given area:

Logs:
info: [api] ::ffff:10.0.0.97 - - [17/Jan/2021:16:45:13 +0000] "GET /v1/nearby?point.lon=2.2056249941050794&size=40&layers=venue&categories=tourism&sort=popularity&boundary.circle.radius=7.065170809924477&point.lat=41.37980261117921 HTTP/1.1" 200 51246
info: [api] elasticsearch controller=search, queryType=reverse, es_hits=721, result_count=80, es_took=101, response_time=113, layers=[venue], size=40, private=false, point.lat=41.393325741087345, point.lon=2.1767000523781235, boundary.circle.radius=7.063700682170273, boundary.circle.lat=41.393325741087345, boundary.circle.lon=2.1767000523781235, categories=[tourism], sort=popularity, name=English, iso6391=en, iso6393=eng, via=header, defaulted=false, querySize=80, retries=0, text_length=0
Any additional feedback or suggestions are welcome.
Hi @bboure, did you launch your project now ? I would like to try this feature in a real use case :smile:
@Joxit Not yet 😞 Some other things with higher priorities got in the way.
It's not dead though! I hope I can came back to it soon!
Will keep you posted