GeoFlutterFire
GeoFlutterFire copied to clipboard
Question: Is the within() method compatible with other sorts and limits
Hey, could I use the within query with a limit and sort? For example could I get the top 50 best rated locations (rating being an extra attribute) by chaining a sort and a limit to the within method?
Also, would this produce 50 reads in firestore? Or would it produce one read for each result of within(), regardless of how many results I filter out by limit?
Thanks a lot.
From what I can see, you can't limit or sort a within query. I am looking for a solution for this when I stumbled onto your issue. I'd be curious to know if that is possible from the creator.
Sorry for the delayed response!
- No, sorts are not supported because of the need to perform
orderBy()on geohashes before filtering them out. So, appendingorderBy()with another field would mess with thestartAt()andorderBy()combination used in the library. You can read about this here - https://firebase.google.com/docs/firestore/query-data/order-limit-data limit()is a special case, a hard limit on 50 documents is not possible. But, the next best alternative is limiting by 5. The library basically runs 9 queries in a sequence, i.e 1 geohash in the center and 8 surrounding geohashes, all within the specified radius with some edge cases(mostly because of the square shape). So, you can engineer to limit each query to a hard limit of 5, so the number of reads inside each sub-hash is limited to 9*5 reads overall. This is just an alternative solution to control the number of reads that could happen, but not a completelimit()alternative.
BTW... It appears that within returns records sorted by the distance from the center. I did not see that documented.
@awhitford Thank you for your comment, which saved me the time from testing the ordering of .within() myself. Agreed, it should be documented somewhere.
Do you think a proper limit functionality will presumably be available? Thanks.