DarshanGowda0 / GeoFlutterFire

:fire:GeoFlutterFire:fire: is an open-source library that allows you to store and query firestore documents based on their geographic location.
https://youtu.be/MYHVyl-juUk
MIT License
305 stars 261 forks source link

Composite index not working for query with where #49

Closed ShehabSN closed 5 years ago

ShehabSN commented 5 years ago

When i tried to add another criteria checking for the value of a specific field i get this error to create an index. I have created indexes before and for some reason the link provided NEVER works and always sends me to an error page. So i usually create the indexes manually. This time however, it wouldnt work so i am confused as to where i am going wrong with this? This is the error message i am getting :

Listen for Query(users where atEvent == true order by position.geohash, __name__) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: ```

So atEvent is the name of the field with the boolean and position is a map with geohash and also geopoint, so i tried creating a manual index with atEvent as a field (ascending) and position.geohash as another field (descending) and it wouldnt work. I also tried just using position and atEvent but didnt work either. How would i create this index? The package says that it supports this type of query and gives an example just like mine. This is my code for the query:

stream = radius.switchMap((rad) {
        var collectionReference = Firestore.instance.collection('users').where('atEvent',isEqualTo: true);
        return geo.collection(collectionRef: collectionReference).within(
            center: userLoc, radius: rad, field: 'position');
      });
mike-gallego commented 5 years ago

Did you ever find a solution to this problem? Same issue for weeks but never could figure it out so I did an inefficient workaround of using the queried data and trimming it with if statements such as, in your example, after you listen for the stream and get the list of document data:

if (document.data['atEvent']) { listOfDocs.add(document); }

ShehabSN commented 5 years ago

No i haven't, this needs to be addressed since most apps will need an additional query...

ShehabSN commented 5 years ago

It works when you specify position.geohash ascending as one of your index fields

stevenspiel commented 5 years ago

I think the trick is to pass a CollectionReference to geo.collection. Calling .where() on a CollectionRef returns a Query object, so if you call .reference() on the Query object, it should work. At least, it did for me.

You could do something like:

stream = radius.switchMap((rad) {
        var collectionReference = Firestore.instance.collection('users').where('atEvent',isEqualTo: true);
        return geo.collection(collectionRef: collectionReference.reference()).within(
            center: userLoc, radius: rad, field: 'position');
      });

Another person had a similar question:

https://github.com/DarshanGowda0/GeoFlutterFire/issues/47#issuecomment-532479084

mike-gallego commented 5 years ago

@stevenspiel I am going to have to try that out sometime.

MATTYGILO commented 4 years ago

It works when you specify position.geohash ascending as one of your index fields

Thanks a lot worked