Firestore动态eventId下查询索引创建及FirestoreRecyclerAdapter报错解决
eventId Queries Hey, let's break down how to resolve that FAILED_PRECONDITION: The query requires an index error you're hitting. Your query combines two whereEqualTo filters with an orderBy, which Firestore needs a composite wildcard index for—especially since your events_attended field uses dynamic eventId keys. Here's exactly what to do:
Why You Need This Index
Firestore doesn't automatically create indexes for queries that mix multiple equality filters with sorting. Since your events_attended is a Map field where keys are dynamic eventId values, you can't just create a static index for one specific eventId—you need a wildcard index that covers all possible keys in that Map.
Step-by-Step to Create the Wildcard Composite Index
Open your Firebase Console, navigate to your Firestore database, and go to the Indexes tab.
Click Add Index to start building a new composite index.
Fill in the details correctly:
- Collection path: Enter
EVENT_INFO/{eventId}/USERS(the{eventId}wildcard here tells Firestore to apply this index to allUSERSsubcollections under anyeventIddocument inEVENT_INFO). - Fields:
- First field: Select
age(matching yourFIELD_AGEfilter), set the sort direction to either Ascending or Descending (equality filters don't care about sort direction here). - Second field: Select
events_attended, then click the wildcard icon (the little*next to the field name) to turn it intoevents_attended.*. Set the sort direction to Ascending. This wildcard covers all dynamiceventIdkeys in your Map. - Third field: Select
familyName(matching yourFIELD_FAMILYNAMEsort), set the sort direction to Ascending (to match your query'sorderBydirection).
- First field: Select
- Make sure the Enable checkbox is checked, then click Save.
- Collection path: Enter
Wait a few minutes for Firestore to build the index (you'll see a "Building" status until it's ready). Once it's done, your query should work without the error.
Double-Check Your Query
Just to confirm, here's your original query for reference—make sure your field names and collection paths match exactly what's in your Firestore database (Firestore is case-sensitive!):
Query query = mFirebaseFirestore.collection(EVENT_INFO).document(eventId) .collection(USERS) .whereEqualTo(FIELD_AGE, guestAge) .whereEqualTo(EVENTS_ATTENDED + "."+ eventId, true) .orderBy(FIELD_FAMILYNAME, Query.Direction.ASCENDING);
Troubleshooting Tips
- If you still get the error after creating the index, double-check that your collection path in the index matches the one in your code (no typos!).
- Ensure that
events_attendedis indeed a Map field in your Firestore documents (not an array or other type). - Sometimes Firestore takes a bit longer to propagate new indexes—give it 5-10 minutes if the error persists immediately after saving.
内容的提问来源于stack exchange,提问作者saintPeter

