Search code examples
algorithmgeolocationrestful-architecture

What is the best way to implement a service operation that returns places sorted by distance to the current user?


let's say a service operation like this

api/places/?category=entertainment&geo=123,456&area=30&orderBy=distance

so the user is searching for places of entertainment near the geo location (123,456), no further than 30 kms boundary, and want the results sorted by distance

suppose the search should be paged, say it will have 500 items satisfy the query, but the page size is 50, so it will have 10 pages.

each item in database stores only geo location of the place, and then I will have to fetch all 500 items from db first, calculate the distance of each item, cut the array to the page number and then return.

so every time the user is requesting next page, I will have to query all 500 and then do the same thing again.

is this the right way to implement a service like that, or is there a better strategy?

it seems to be worse when my database don't have the geo location, because I am using a different API provider to give me the geo of a place. It means I will have to query everything and hit another service to get the geo, calculate, and finally able to sort... :(

thank you very much!


Solution

  • If you were developing a single-page app for the search results, you wouldn't need to send another request to the server every time the user presses Next. You could use a pagination library that takes the full set of results and sorts them into pages accordingly.

    In this situation, there's a trade between the size of the data you want to store, and the speed and efficiency of your web application. In this sort of situation, you should really be dealing with large data sets. You should ideally have additional databases for each general geographic region (such as Northeast, Southeast) that store the distance between each store and each location the user can enter. You should use a separate server for this, and aggregate the data at intervals (say, every six hours) using an automated database operation, such as running MongoDB scripts.

    You should also consider using weighted graphs to store the distances between locations. You could then more easily traverse them with a graphing algorithm.