d-_-b
d-_-b

Reputation: 23191

Firebase index required but no link provided

Tip: instead of creating indexes here, run queries in your code – if you're missing any indexes, you'll see errors with links to create them. Learn more

I'm getting an error no matching index found without the usual direct link to create the index required.

enter image description here

My database is structured as follows:

answers (collection)
  - $docId (document)
      - $refId (collection)
          - $docId (document)
              - created_at (timestamp)
              - type (string)

My query is:

firebase.firestore()
 
 .collection('answers')
 .doc('abcdHajM930Gabcd')
 .collection('abcdetIo25xYqAabcd')
 .orderBy("created_at", "desc")
 .limit(5)
 .where("type", "==", "3")

I've tried all combinations of indexes I could think of (screenshot below), What am I doing wrong?

collection field type
answers created_at: DESC type: ARRAY
answers created_at: DESC type: ARRAY
answers created_at: DESC type: ASC
answers created_at: DESC type: ASC
answers type: ASC created_at: DESC
answers type: ASC created_at: DESC

enter image description here

Upvotes: 4

Views: 1452

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50910

Update: The issue seems to be fixed and Javascript client SDK now returns the index creation link.


I'm not totally sure if that's intended by the Firebase JS SDK or no, but if you try running your query in a Cloud Function (just use the emulator) then it should throw an error containing the link to create the index.

export const getIndexLink = functions.https.onRequest(async (request, response) => {
    //   functions.logger.info("Hello logs!", {structuredData: true});
    try {
        const snap = await admin.firestore().collection('answers')
            .doc('abcdHajM930Gabcd')
            .collection('abcdetIo25xYqAabcd')
            .where("type", "==", "3")
            .orderBy("created_at", "desc")
            .limit(5).get()
        console.log(snap.size)
        response.send("Hello from Firebase!" + snap.size);
    } catch (error) {
        console.log(error)
        response.send(error.message)
    }
});

I tried copying your query and created an index using the URL and the index turns out to be:

enter image description here

Your fields that your are using are not a part of documents of "answers" collection. Now if you change the sub-collection name, it'll require another index. What does your Firestore structure look like? I'd recommend restructuring it a bit and get a same sub-collection name and create an index with collectionGroup scope.

Upvotes: 4

Related Questions