تنشئ Cloud Firestore تلقائيًا فهارس لإتاحة الأنواع الأكثر شيوعًا من طلبات البحث، ولكنها تسمح لك بتحديد الفهارس المخصّصة وعمليات إلغاء الفهارس على النحو الموضَّح في أدلة Cloud Firestore.
ويمكنك إنشاء فهارس مخصّصة وتعديلها ونشرها في وحدة تحكُّم Firebase أو باستخدام واجهة سطر الأوامر. من واجهة سطر الأوامر، عدِّل ملف إعداد الفهرس، مع تحديد
اسم الملف التلقائي firestore.indexes.json
، ثم انشره باستخدام الأمر firebase
deploy
.
يمكنك تصدير الفهارس من خلال واجهة سطر الأوامر باستخدام firebase firestore:indexes
.
يحدّد ملف إعداد الفهرس عنصرًا واحدًا يحتوي على
مصفوفة indexes
ومصفوفة fieldOverrides
اختيارية.
وفي ما يلي مثال لذلك:
{
// Required, specify compound and vector indexes
indexes: [
{
collectionGroup: "posts",
queryScope: "COLLECTION",
fields: [
{ fieldPath: "author", arrayConfig: "CONTAINS" },
{ fieldPath: "timestamp", order: "DESCENDING" }
]
},
{
collectionGroup: "coffee-beans",
queryScope: "COLLECTION",
fields: [
{
fieldPath: "embedding_field",
vectorConfig: { dimension: 256, flat: {} }
}
]
}
],
// Optional, disable indexes or enable single-field collection group indexes
fieldOverrides: [
{
collectionGroup: "posts",
fieldPath: "myBigMapField",
// We want to disable indexing on our big map field, and so empty the indexes array
indexes: []
}
]
}
نشر تهيئة فهرس
انشر إعدادات الفهرس باستخدام الأمر firebase deploy
. إذا كنت تريد نشر الفهارس لقواعد البيانات التي تم ضبطها في مشروعك فقط، أضِف العلامة --only firestore
. اطّلِع على مرجع الخيارات لهذا الأمر.
لعرض الفهارس المنشورة، شغِّل الأمر firebase firestore:indexes
. أضِف العلامة --database=<databaseID>
لإدراج فهارس لقاعدة بيانات أخرى غير قاعدة البيانات التلقائية لمشروعك.
إذا أجريت تعديلات على الفهارس باستخدام "وحدة تحكُّم Firebase"، تأكَّد أيضًا من تعديل ملف الفهارس المحلية. لمزيد من المعلومات حول إدارة الفهارس، يمكنك الاطّلاع على أدلة Cloud Firestore.
تنسيق JSON
الفهارس
في ما يلي مخطط عنصر واحد في صفيف indexes
. يتمّ تحديد السمات الاختيارية باستخدام الحرف ?
.
يُرجى العِلم أنّه لا يمكن فهرسة حقول مستندات Cloud Firestore إلا في وضع واحد،
وبالتالي لا يمكن أن يحتوي عنصر الحقل إلا على إحدى السمات order
وarrayConfig
وvectorConfig
.
collectionGroup: string // Labeled "Collection ID" in the Firebase console
queryScope: string // One of "COLLECTION", "COLLECTION_GROUP"
fields: array
fieldPath: string
order?: string // One of "ASCENDING", "DESCENDING"; excludes arrayConfig and vectorConfig properties
arrayConfig?: string // If this parameter used, must be "CONTAINS"; excludes order and vectorConfig properties
vectorConfig?: object // Indicates that this is a vector index; excludes order and arrayConfig properties
dimension: number // The resulting index will only include vectors of this dimension
flat: {} // Indicates the vector index is a flat index
FieldOverrides
في ما يلي مخطط عنصر واحد في صفيف fieldOverrides
. يتمّ تحديد السمات الاختيارية باستخدام الحرف ?
.
يُرجى العلم أنّه لا يمكن فهرسة حقول مستندات Cloud Firestore إلا في وضع واحد، وبالتالي لا يمكن أن يحتوي كائن الحقل على السمتَين order
وarrayConfig
.
collectionGroup: string // Labeled "Collection ID" in the Firebase console
fieldPath: string
ttl?: boolean // Set specified field to have TTL policy and be eligible for deletion
indexes: array // Use an empty array to disable indexes on this collectionGroup + fieldPath
queryScope: string // One of "COLLECTION", "COLLECTION_GROUP"
order?: string // One of "ASCENDING", "DESCENDING"; excludes arrayConfig property
arrayConfig?: string // If this parameter used, must be "CONTAINS"; excludes order property
سياسة مدة البقاء (TTL)
يمكن تفعيل سياسة TTL أو إيقافها باستخدام مصفوفة fieldOverrides
على النحو التالي:
// Optional, disable index single-field collection group indexes
fieldOverrides: [
{
collectionGroup: "posts",
fieldPath: "ttlField",
ttl: "true", // Explicitly enable TTL on this Field.
// Disable indexing so empty the indexes array
indexes: []
}
]
للاحتفاظ بالفهرسة التلقائية في الحقل وتفعيل سياسة مدة البقاء:
{
"fieldOverrides": [
{
"collectionGroup": "yourCollectionGroup",
"fieldPath": "yourFieldPath",
"ttl": true,
"indexes": [
{ "order": "ASCENDING", "queryScope": "COLLECTION_GROUP" },
{ "order": "DESCENDING", "queryScope": "COLLECTION_GROUP" },
{ "arrayConfig": "CONTAINS", "queryScope": "COLLECTION_GROUP" }
]
}
]
}
لمزيد من المعلومات عن سياسات مدة البقاء (TTL)، يُرجى مراجعة المستندات الرسمية.