GeoFlutterFire icon indicating copy to clipboard operation
GeoFlutterFire copied to clipboard

Question: Is the within() method compatible with other sorts and limits

Open philipfeldmann opened this issue 6 years ago • 5 comments

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.

philipfeldmann avatar Mar 18 '19 15:03 philipfeldmann

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.

davydany avatar Mar 22 '19 21:03 davydany

Sorry for the delayed response!

  • No, sorts are not supported because of the need to perform orderBy() on geohashes before filtering them out. So, appending orderBy() with another field would mess with the startAt() and orderBy() 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 complete limit() alternative.

DarshanGowda0 avatar May 04 '19 19:05 DarshanGowda0

BTW... It appears that within returns records sorted by the distance from the center. I did not see that documented.

awhitford avatar May 04 '19 23:05 awhitford

@awhitford Thank you for your comment, which saved me the time from testing the ordering of .within() myself. Agreed, it should be documented somewhere.

travisjayday avatar May 29 '20 17:05 travisjayday

Do you think a proper limit functionality will presumably be available? Thanks.

justChris avatar Aug 24 '20 08:08 justChris