GeoFlutterFire
GeoFlutterFire copied to clipboard
Where / Compound Query Not Returning
I've been struggling with a bit of a bug where no data is being returned as soon as I use a QueryRef instead of the CollectionRef.
Issue When using a QueryRef and the composite index is not created, no data is returned and errors are logged.
What is expected An error saying an index needs to be created first. Apparently the url to generate this index used to bubble up, but this is not the case anymore.
Here is an example of the issue
Working
var query = Firestore.instance.collection('seekers');
var snap = await _geo
.collection(collectionRef: query)
.within(center: _geo.point(latitude: lat, longitude: lon), radius: locationRadius, field: location', strictMode: false)
.first;
return snap;
Not Working
var query = Firestore.instance.collection('seekers').where('position', isEqualTo: 'none');
var snap = await _geo
.collection(collectionRef: query)
.within(center: _geo.point(latitude: lat, longitude: lon), radius: locationRadius, field: location', strictMode: false)
.first;
return snap;
Solution Add a composite index on Firebase including the where fields, and the geohash field - eg position: asc, location.geohash: asc
I've also been running into this.
This worked for me as soon as I called .reference()
on the query to return a DocumentReference.
So, in theory, this should work for you:
var query = Firestore.instance.collection('seekers').where('position', isEqualTo: 'none');
var snap = await _geo
.collection(collectionRef: query.reference())
.within(center: _geo.point(latitude: lat, longitude: lon), radius: locationRadius, field: location', strictMode: false)
.first;
return snap;
@stevenspiel It looks like the order of the keys in the composite index is important (location.geohash must be last in the list?) I managed to do it without .reference().
@rolznz Can you please explain how you managed to do this ? Thanks
Also upvoting this. I am not getting the prompt with the link to create the composite index. Without it, it is returning data but ignoring the composite query (where clauses).
@atnegrete I am facing the same issue as you. My where clause is being ignored when using
collectionRef: query.reference()
which results in returning everything
@DarshanGowda0 can you please take a look into the issue? where clause was working perfectly fine back in the days but not it seems to be giving issues now
@stevenspiel are you sure
collectionRef: query.reference()
is working?
because it's returning all the results because the .where() is being ignored
ok the problem for me has been solved for me.
apparently the solution was not
collectionRef: query.reference()
as stated by @stevenspiel
The solution was to manually create an index on firebase console.
usually when using the standard cloud firestore plugin, it would prompt you with a link to automatically make the index if needed. But in this case with this package, the message wasn't passed.
The solution to manually create the index.
more info on the solution is here:
https://github.com/DarshanGowda0/GeoFlutterFire/issues/29
@DarshanGowda0
Take note that there was no prompt to create index given as stated that it should have on your documentation
https://pub.dev/packages/geoflutterfire
@momoDragon About to try this right now. No matter what I did, I can't get the error with the link to create the index on the console. I even wrapped it all with a try-catch and nothing!
@atnegrete i don't think its an error. it's a prompt to create the index. that is why a try-catch won't be executed because it is not an error. The prompt is from the cloud firestore plugin and not from the geoflutterfire probably the prompt is not being passed from the cloudfirestore plugin to the geoflutterfire fire plugin, hence we do no get the prompt being shown.
Interesting!
Things are somewhat working as expected manually creating that index. I have noticed that I do not get updates instantly, flipping the "strict mode" flag true/false will cause it to fetch the newer values. Probably a different issue altogether, maybe due to converting that stream into a Future (first) and something keeping the values cached on that stream?
Did any of you manage to succeed using arrayContains or arrayContainsAny in combination with GeoFlutterFire and appropriate composite index?
I have created a composite index but for some weird reason it isn't working.
Composite Index My composite Index
Stream
queryStream = queryRadius.switchMap((rad) {
var collectionReference = _firestore.collection('Broadcasts').where('event-category', isEqualTo: 'Entertainment');
var stream = geo.collection(collectionRef: collectionReference).within(center: center, radius: rad, field: 'position', strictMode: true);
return stream;
}
Am I doing something wrong?
@The24HourClock I think there is a conflict in the name of the field (event-category) in the query and index that you have created. There is '-' in the field name but in index there isn't and also 'c' is Capital in index.
Did any of you manage to succeed using arrayContains or arrayContainsAny in combination with GeoFlutterFire and appropriate composite index?
I'm highly interested to know if it is possible or not. I'm currently trying to get result with an arrayContainsAny but cannot get anything.
Ok sorry it is solved for me. My index was incorrect. It is working fine now.
Wrapping the entire thing in try catch worked for me (print the error and you'll get the link to build the index automatically )
This issue is still there and quite annoying. If you have complex queries, it is a pain to build all indexes manually. @DarshanGowda0, is there not a way to pass Cloud console message properly through library? anyone having a clue?
@Bader-Al how did you manage to get output? If I put try catch as follow I don't get anything:
var query = Firestore.instance.collection('seekers').where('position', isEqualTo: 'none');
try {
var snap = await _geo
.collection(collectionRef: query)
.within(center: _geo.point(latitude: lat, longitude: lon), radius: locationRadius, field: 'location', strictMode: false);
} catch (e) {...}
I have a more complicated query, therefore I would like to see the index that I need to create as I'm not managing to make it work.
hey i cant get .where and limit results...????? any luck anyone??