Firestore Indexes

فهارس Firestore

Intermediatedatabase1 min read
composite indexFirestore composite indexcompound indexfirestore.indexes.json

Definition

Database indexes required for any Firestore query that filters or orders by more than one field — without them the query fails at runtime.

فهارس قاعدة البيانات المطلوبة لأي استعلام Firestore يُصفّي أو يُرتّب حسب أكثر من حقل واحد — بدونها يفشل الاستعلام في وقت التشغيل.

Why It Matters

Every time you add a new Firestore query in 404Fault that filters by two fields (e.g., `status == 'published' AND category == 'ai-ml'`), you must add the corresponding composite index. Missing this is a silent failure — the query works in development (small data) but fails in production (large data) because Firestore enforces index requirements more strictly at scale.

في كل مرة تُضيف استعلام Firestore جديداً في 404Fault يُصفّي حسب حقلين (مثل `status == 'published' AND category == 'ai-ml'`)، يجب إضافة الفهرس المركّب المقابل. إغفال هذا هو فشل صامت — يعمل الاستعلام في التطوير (بيانات صغيرة) لكن يفشل في الإنتاج (بيانات كبيرة) لأن Firestore تُطبّق متطلبات الفهرس بصرامة أكبر على نطاق واسع.

Full Definition

Firestore requires an explicit composite index for any query that uses multiple `where()` clauses, or that combines `where()` with `orderBy()`. Single-field queries use automatic indexes. For compound queries, you define indexes in `firestore.indexes.json` and deploy them with `firebase deploy --only firestore:indexes`. Missing indexes cause a runtime error with a link to create the index in the Firebase console — but waiting for index building in production (it can take minutes) is bad practice. Define all needed indexes before deployment.
تتطلب Firestore فهرساً مركّباً صريحاً لأي استعلام يستخدم عبارات `where()` متعددة أو يجمع `where()` مع `orderBy()`. الاستعلامات ذات الحقل الواحد تستخدم فهارس تلقائية. للاستعلامات المركّبة، تُعرّف الفهارس في `firestore.indexes.json` وتُنشرها بـ `firebase deploy --only firestore:indexes`. الفهارس المفقودة تُسبّب خطأ في وقت التشغيل مع رابط لإنشاء الفهرس في Firebase console — لكن انتظار بناء الفهرس في الإنتاج (قد يستغرق دقائق) ممارسة سيئة. عرّف جميع الفهارس المطلوبة قبل النشر.

Example Usage

`firestore.indexes.json`: `{ "indexes": [{ "collectionGroup": "glossary", "queryScope": "COLLECTION", "fields": [{ "fieldPath": "status", "order": "ASCENDING" }, { "fieldPath": "category", "order": "ASCENDING" }, { "fieldPath": "nameEn", "order": "ASCENDING" }] }] }` — enables `where('status', '==', 'published').where('category', '==', 'ai-ml').orderBy('nameEn')`.

`firestore.indexes.json`: `{ "indexes": [{ "collectionGroup": "glossary", "queryScope": "COLLECTION", "fields": [{ "fieldPath": "status", "order": "ASCENDING" }, { "fieldPath": "category", "order": "ASCENDING" }, { "fieldPath": "nameEn", "order": "ASCENDING" }] }] }` — يُمكّن `where('status', '==', 'published').where('category', '==', 'ai-ml').orderBy('nameEn')`.

Knowledge Graph

Avoid these mistakes when using Firestore Indexes:

1

Adding indexes after deployment when users are already hitting the query — always test all queries locally with the emulator and define indexes first

2

Not deleting unused indexes — Firestore charges for index storage and write throughput on indexed fields

3

Using `in` queries with arrays larger than 30 — Firestore's `in` operator is limited to 30 values per query

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using Firestore Indexes.

Explain:
1. What is Firestore Indexes and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Adding indexes after deployment when users are already hitting the query — always test all queries locally with the emulator and define indexes first, Not deleting unused indexes — Firestore charges for index storage and write throughput on indexed fields, Using `in` queries with arrays larger than 30 — Firestore's `in` operator is limited to 30 values per query
5. Best practices and production tips

Official Resources