GeoFlutterFire
GeoFlutterFire copied to clipboard
Issue when using where() to filter causing extra reads
Code
collectionRef: Firestore.instance .collection('vendors') .where('storeType', isEqualTo: 'Cafe') .where('vendorType', isEqualTo: 2).where('block', isEqualTo: false) .where('applicationStatus', isEqualTo: 3).where('isVisible', isEqualTo: true)
Scenario
As u can see i am using where(). However its causing me extra reads.
I have 100 items in 10km radius, but only one of them meets the criteria
.where('storeType', isEqualTo: 'Cafe)
But this caused me 100 reads.
I believe each .where()
is basically an OR
statement that is being merged when they arrive, so it kinda makes sense that it results in multiple reads.
Not possible to incorporate .where() when doing the geoquery rather than do it after the query? Because without its, its not very flexible for doing querys hence causing extra unnecessary doc reads
@momoDragon .where()
is supposed to just give you 1 read, I'm sure this has been the case from the first version when used with isEqualTo
, it uses the same query as cloud_firestore. Could you verify again?
I'm surprised you got a read at all. I tried filtering my collection with a where clause and it seemed to have not work. I checked multiple times to see if it is right collection, field, and value and they were all correct, but still no read. When I would take the where clause out, all the records come through.
@mikeyyg96 .where()
works only with isEqualTo
at the moment. And also are you creating index the first time?
@DarshanGowda0 Yeah I used the exact format in the documentation:
var queryRef = _firestore.collection('locations').where('city', isEqualTo: 'bangalore');
but with my values:
var queryRef = _firestore.collection('events').where('category', isEqualTo: 'activities');
I have this field in my collection and that value as well but nothing returns. When I take out the where clause, the record pops up. It is only 1 record in my collection.
Yes i have tested. and ya. its quite bad. its causing me hell alot of reads man. @DarshanGowda0 u need to try it again man. Open a new project and use app engine to track the usage.
@DarshanGowda0 Yeah I used the exact format in the documentation:
var queryRef = _firestore.collection('locations').where('city', isEqualTo: 'bangalore');
but with my values:var queryRef = _firestore.collection('events').where('category', isEqualTo: 'activities');
I have this field in my collection and that value as well but nothing returns. When I take out the where clause, the record pops up. It is only 1 record in my collection.
Check your console. it would prompt u to create index. there will be a link. just click it
@momoDragon Can you please show me where that link is located or what exactly is it saying? I have a lot of information in my console and it keeps updating and filtering the console with vscode sucks.
Edit: BTW, did you try strictMode: true
? Maybe that'll help.
@mikeyyg96 yup. I always use strict mode
Hey @momoDragon! I verified this again, the number of reads is always 1 if I have just one document that satisfies the where()
condition. The firestore query you pass is the first condition that is executed and geofire filtering happens after the where()
filter is done (check this).
I have sort of a similar issue. When I use a where clause the stream never returns back:
var query = Firestore.instance
.collection('itineraries')
.where('status', isEqualTo: status.toString());
GeoFirePoint center =
geo.point(latitude: nearby.latitude, longitude: nearby.longitude);
double radius = 50;
String field = 'position';
Stream<List<DocumentSnapshot>> stream = geo
.collection(collectionRef: query)
.within(center: center, radius: radius, field: field);
List<DocumentSnapshot> docs = await stream.first;
if I remove the where it works fine. I have created the composite index in different ways manually as I never get the link to create it
Am I missing something here?
Same issue as rmelian's. Like rmelian, I never get the link to create the composite index.
@rmelian Solved by manually creating an index that, in your terms, would be: Collection ID: itineraries Field indexed: status Ascending position.geohash Ascending Query scope: Collection
i have sort of the same issue:
var collectionRef = Firestore.instance.collection('/user_data').where('gender', isEqualTo: 'male');
return geo .collection(collectionRef: collectionRef.reference()) .within(center: center, radius: 1, field: 'location');
it totally ignores the .where, and just outputs all users within the radius.
@rmelian Solved by manually creating an index that, in your terms, would be: Collection ID: itineraries Field indexed: status Ascending position.geohash Ascending Query scope: Collection
is need sequins as where conditions and then position.geohash index look like status_position.geohash
@atozbtcom and @Moellendorff are right. I just verified that. @yevgeniaronov u need to manually set indexing in firebase console.
ensure that u sort the where clause field first before the position.geohash
@momoDragon So position.geohash must be the last field when setting up the index?
Update - That did not work for me either
query still not working after creating composite index. did anyone find a solution for this? Thank you!