MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.
https://geofirestore.com
MIT License
504 stars 58 forks source link

Query returning zero results when filtering with where #223

Closed smolugu closed 2 years ago

smolugu commented 2 years ago

I am using geofirestore ^3.4.1. Have created an index when prompted but the query returns zero results.

const query = userCollectionRef.where('tags', 'array-contains-any', keys). near({ center: gp, radius: radius, });

can anyone tell me what I could be missing?

Regards,

MichaelSolati commented 2 years ago

Usually, not always, this is an indexing issue (aka a missing index)

1) What do your indexes look like? 2) I'm not seeing you resolve this query... near returns a geoquery object which you'll need to call onSnapshot or get on. Assuming you're doing one of these, can you add an error catch to it and share the results if it logs an error? If it is an indexing error this should log and provide a link to generate the correct index.

smolugu commented 2 years ago

Hi, The index is created on d.tags Arrays, g Ascending.

I am using get on the query. There is no error in the logs and the docs in the resulting querySnapshot is always empty.

I tried to create the index manually but it is not recognized.

Have also tried deleting the index and creating it again multiple times.

const query = userCollectionRef.where('tags', 'array-contains-any', keys).
                near({
                    center: gp,
                    radius: radius,
                });
            try {
                const value = await query.get();
                console.log('query result: ' + value);
                const userDocs = value.docs;
                console.log('userDocs length: ' + userDocs.length);
                console.log('userDocs: ' + userDocs);
                userDocs.forEach(element => {
                    console.log('element value:' + element.data().id);
                });
            } catch (err) {
                console.log('geofirestore error: ' + err);
            }
MichaelSolati commented 2 years ago

Can you share a screenshot of what a doc in your collection looks like, also are you using geofirestore to add the doc to the collection?

smolugu commented 2 years ago

I am using GeoFlutterFire in flutter app to add location info to the doc. The fields look as in the below screenshot. The indexes which I created when prompted while fetching records in the app are working fine. (the index is created as: tags Arrays geoLocation.geohash Ascending)

will this be an issue? It never occurred to me as the regular queries worked fine in cloud function.

Screen Shot 2021-12-22 at 9 08 18 AM
smolugu commented 2 years ago

@MichaelSolati I am planning on creating a separate collection adding documents with geofirestore.

Also, just want to know if we can add a map with field name "d" and keys on which we want to create a composite index.

spoxies commented 2 years ago

Just a note: In my case(s) the queries return nothing, only if 'array-contains' or similair is used in the where() (but I'm on a different version). Currently trying to make sense of it.

The fun thing that firebase indeed asks to make a compound index for the 'contains' type when I first run it. So it does get passed and does 'things'.

Works: const query = userCollectionRef.near({ center: gp, radius: radius}).where('deleted', '==', false);

Does not work: const query = userCollectionRef.near({ center: gp, radius: radius}).where('_indexChannel', 'array-contains', keys);

The created index looks like this:

{
      "collectionGroup": "invite",
      "queryScope": "COLLECTION",
      "fields": [
        {
          "fieldPath": "d._indexChannel",
          "arrayConfig": "CONTAINS"
        },
        {
          "fieldPath": "g",
          "order": "ASCENDING"
        }
      ]
    },

But I'm curious if it is the same for you @smolugu ?

ZacharyHandshoe commented 1 year ago

Any resolution for this?

spoxies commented 1 year ago

@ZacharyHandshoe It has been a while but what I note when looking to my code I have two almost exact indexes but one with g and the other with g.geohash that might both be needed to facilitate such query:

Query

return geoUsersCollection
                .near({ center: geoPoint, radius: geoRadius })
                .where('deleted', '==', false)
                .where('subscriptions', 'array-contains', channel)
                .get()

Indexes:

    {
      "collectionGroup": "users",
      "queryScope": "COLLECTION",
      "fields": [
        {
          "fieldPath": "subscriptions",
          "arrayConfig": "CONTAINS"
        },
        {
          "fieldPath": "deleted",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "g.geohash",
          "order": "ASCENDING"
        }
      ]
    },
 {
      "collectionGroup": "users",
      "queryScope": "COLLECTION",
      "fields": [
        {
          "fieldPath": "deleted",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "subscriptions",
          "arrayConfig": "CONTAINS"
        },
        {
          "fieldPath": "g",
          "order": "ASCENDING"
        }
      ]
    },

If that is NOT the solution it might be working on my side because I'm running a version of geofirestore-js that implements a patched version of the geofirebase-core. Reasons explained here: https://github.com/MichaelSolati/geofirestore-core/issues/6 But as said it has been a while but it might be a direction to pursue.