Cloud Firestore จะสร้างดัชนีโดยอัตโนมัติเพื่อรองรับประเภทคำค้นหาที่พบบ่อยที่สุด แต่คุณจะกำหนดดัชนีที่กำหนดเองและการลบล้างดัชนีได้ตามที่อธิบายไว้ในคู่มือสำหรับ Cloud Firestore
คุณจะสร้าง แก้ไข และติดตั้งใช้งานดัชนีที่กำหนดเองได้ในคอนโซล Firebase หรือใช้ CLI จาก CLI ให้แก้ไขไฟล์การกำหนดค่าดัชนีซึ่งมีชื่อไฟล์เริ่มต้น firestore.indexes.json
และทำให้ใช้งานได้โดยใช้คำสั่ง firebase
deploy
คุณส่งออกดัชนีที่มี CLI ได้โดยใช้ firebase firestore:indexes
ไฟล์การกำหนดค่าดัชนีกำหนดออบเจ็กต์ 1 รายการที่มีอาร์เรย์ 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
ดัชนี
สคีมาสำหรับออบเจ็กต์ 1 รายการในอาร์เรย์ indexes
มีดังนี้ พร็อพเพอร์ตี้ที่ไม่บังคับจะระบุด้วยอักขระ ?
โปรดทราบว่าช่องเอกสาร Cloud Firestore จะจัดทำดัชนีได้ในโหมดเดียวเท่านั้น ออบเจ็กต์ช่องจึงมีพร็อพเพอร์ตี้ order
, arrayConfig
และ vectorConfig
ได้เพียง 1 รายการ
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
สคีมาสำหรับออบเจ็กต์ 1 รายการในอาร์เรย์ 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: []
}
]
หากต้องการใช้การจัดทำดัชนีเริ่มต้นในช่องและเปิดใช้นโยบาย TTL ให้ทำดังนี้
{
"fieldOverrides": [
{
"collectionGroup": "yourCollectionGroup",
"fieldPath": "yourFieldPath",
"ttl": true,
"indexes": [
{ "order": "ASCENDING", "queryScope": "COLLECTION_GROUP" },
{ "order": "DESCENDING", "queryScope": "COLLECTION_GROUP" },
{ "arrayConfig": "CONTAINS", "queryScope": "COLLECTION_GROUP" }
]
}
]
}
ดูข้อมูลเพิ่มเติมเกี่ยวกับนโยบาย Time to Live (TTL) ได้ที่เอกสารอย่างเป็นทางการ