การอ้างอิงคำจำกัดความดัชนี Cloud Firestore

Cloud Firestore จะสร้างดัชนีโดยอัตโนมัติเพื่อรองรับประเภทการสืบค้นที่พบบ่อยที่สุด แต่ช่วยให้คุณกำหนดดัชนีที่กำหนดเองและการแทนที่ดัชนีตามที่อธิบายไว้ใน คำแนะนำของ Cloud Firestore

คุณสามารถสร้าง แก้ไข และปรับใช้ดัชนีที่กำหนดเองในคอนโซล Firebase หรือใช้ CLI จาก CLI ให้แก้ไขไฟล์การกำหนดค่าดัชนีของคุณด้วยชื่อไฟล์เริ่มต้น firestore.indexes.json และปรับใช้โดยใช้คำสั่ง firebase deploy

คุณสามารถส่งออกดัชนีด้วย CLI โดยใช้ firebase firestore:indexes

ไฟล์การกำหนดค่าดัชนีกำหนดหนึ่งออบเจ็กต์ที่มีอาร์เรย์ indexes และอาร์เรย์ fieldOverrides ที่เป็นตัวเลือก นี่คือตัวอย่าง:

{
  // Required, specify compound indexes
  indexes: [
    {
      collectionGroup: "posts",
      queryScope: "COLLECTION",
      fields: [
        { fieldPath: "author", arrayConfig: "CONTAINS" },
        { fieldPath: "timestamp", order: "DESCENDING" }
      ]
    }
  ],

  // 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

  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 property
    arrayConfig?: string   // If this parameter used, must be "CONTAINS"; excludes order property

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 สามารถเปิดหรือปิดใช้งานได้โดยใช้อาร์เรย์ 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) โปรดอ่าน เอกสารอย่างเป็นทางการ