GeoFlutterFire icon indicating copy to clipboard operation
GeoFlutterFire copied to clipboard

Where / Compound Query Not Returning

Open developcodeza-matthew opened this issue 5 years ago • 19 comments

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

developcodeza-matthew avatar Jul 01 '19 13:07 developcodeza-matthew

I've also been running into this.

stevenspiel avatar Sep 18 '19 01:09 stevenspiel

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 avatar Sep 18 '19 01:09 stevenspiel

@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 avatar Oct 28 '19 06:10 rolznz

@rolznz Can you please explain how you managed to do this ? Thanks

jchapuy avatar Nov 02 '19 17:11 jchapuy

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 avatar Dec 26 '19 17:12 atnegrete

@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

momoDragon avatar Dec 31 '19 03:12 momoDragon

@stevenspiel are you sure collectionRef: query.reference() is working? because it's returning all the results because the .where() is being ignored

momoDragon avatar Dec 31 '19 03:12 momoDragon

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 Screenshot 2020-01-01 at 11 32 29 AM

momoDragon avatar Jan 01 '20 03:01 momoDragon

@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 avatar Jan 02 '20 06:01 atnegrete

@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.

momoDragon avatar Jan 02 '20 06:01 momoDragon

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?

atnegrete avatar Jan 02 '20 06:01 atnegrete

Did any of you manage to succeed using arrayContains or arrayContainsAny in combination with GeoFlutterFire and appropriate composite index?

joknjokn avatar Jan 08 '20 08:01 joknjokn

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?

TommySarkissian avatar Aug 16 '20 10:08 TommySarkissian

@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.

yashpaneliya avatar Sep 05 '20 10:09 yashpaneliya

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.

daseed avatar Oct 23 '20 19:10 daseed

Wrapping the entire thing in try catch worked for me (print the error and you'll get the link to build the index automatically )

Bader-Al avatar Feb 14 '21 10:02 Bader-Al

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?

davemg3 avatar Jul 18 '21 15:07 davemg3

@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.

J4NN0 avatar Jul 28 '21 15:07 J4NN0

hey i cant get .where and limit results...????? any luck anyone??

s1dc0des avatar Oct 13 '21 16:10 s1dc0des