fluttercommunity / firestore_helpers

Firestore Helpers - Firestore helper function to create dynamic and location based queries. Maintainer: @escamoteur
https://pub.dev/packages/firestore_helpers
MIT License
48 stars 19 forks source link

Actual Number of Document Reads #9

Closed JCPradelPH closed 6 years ago

JCPradelPH commented 6 years ago

Hi,

We are currently building an app using Flutter and one of our key functionality is getting nearby Restaurant records within the area of the mobile user's location from Firestore. Our main problem is that if we have thousands of restaurant documents, then it will be an expensive operation given that Firestore charges by the number of documents retrieved. We need to only query the nearby records and not query all the records only to perform the computation of distance one by one.

If we implement your location based query sample, are we only going to be charged with the number of results(only the queried nearby restaurants) that it returns? For example, we have 10,000 restaurants/documents in our collection but only 100 restaurants/documents are actually nearby, are we going to be charged for only 100 reads? Our main concern is that maybe this library also queries all the documents in a collection and then also performs the calculation per document.

Thanks

escamoteur commented 6 years ago

Hi, The location based queries either by using GetDataInArea or by creating a location constraints and use get data are executed on the server with one small caveat. It's only possible to query for locations inside a square that encompasses a circle with the desired radius. Which means the server will return also entries that are in the corners of the square but outside the radius. That entries are then filtered out on the client side.

JCPradelPH commented 6 years ago

Okay so lets say that I have 100 documents and then with my given radius there are only 30 documents inside the square encompassing my radius, will Firestore only charge me for 30 document reads?

escamoteur commented 6 years ago

All I can say that its a serverbased query and that only that data should be transmitted. You have to check the payment details yourself

JCPradelPH commented 6 years ago

Thanks for the quick response it is very much appreciated. We are aware of the pricing details, we were just asking how your library filter the results. Is it whether your querying all the documents inside the collection and loops through those documents to perform the filters needed or do you implement some kind of geohashing to avoid querying all the documents within the collection.

escamoteur commented 6 years ago

I thought I already wrote that above. I query all documents in a given geografical rectangle based on an greater / less than query on geopoints on the server side.

JCPradelPH commented 6 years ago

What we mean is are you performing the query like this:

const businesses = await firebaseAdmin.firestore().collection("businesses").get()
// perform logic for nearby location

or like this

const businesses = await firebaseAdmin.firestore().collection("businesses")
     .where("location", "==", geoLoc).get()

We just want to verify how are you fetching records from firestore because the first approach is pretty expensive in read operations.

Thanks in advance.

escamoteur commented 6 years ago

The second one which is meant if you say server side query. Also you can always look into the code

JCPradelPH commented 6 years ago

Great! Thanks for clearing that out. Cheers!