Closed adevine closed 5 years ago
I don't necessarily want to add a distance sort, as it's something that you as a developer can easily do, and it adds an extra layer to the library (which depending on your query size can slow down the result). Keep in mind we do a filter each time a query emits new values, and on average one geoquery is about 8 firestore queries covering the area you want.
There is however a little "hack" you could do... The limit
method on a geoquery DOES sort the documents so that we ensure we show you the closest documents. So just add... .limit(1000000)
(or some other absurdly high value) to your query and we will sort for you...
Thanks for the response @MichaelSolati . I understand your reasoning for not adding sort, but just wanted to comment that I don't believe your statement about using limit
is correct, so don't want someone else to try it and then get stuck:
It's that #2 behavior that I find particularly strange. Ideally I think it would be best if setting a limit DID return the query docs in sorted order, but that doesn't appear to be the case.
Well I'm glad someone knows my work better than me 😅, it's been a while since I've looked at that part, and you're absolutely right! So I can't explain my reasoning for not returning the limited docs sorted... I guess I didn't see any reason for it as I imagined a user expected just a limit
on items returned and I thought it made sense to limit based on order, but not to pass the order to a user. I'm open to this being changed, but I don't see it being something I'll be rushing to do (I'm open to a PR for this though).
For those of you who do need their snapshots sorted, heres a lil code for ya:
geoFirestore.collection('retaurants')
.near({ center: new firebase.firestore.GeoPoint(1, 2), radius: 1000 })
.onSnapshot((snapshot) => {
const sorted = snapshot.docs.sort((a, b) => a.distance - b.distance);
});
When a "near" query is used with a center and radius, would be ideal to add an option to sort results by distance from the center. Right now the distance field is placed on the docs that come back, but the results are not sorted by distance.