Cloud Firestore 색인 정의 참조

Cloud Firestore는 가장 일반적인 유형의 쿼리를 지원하기 위해 자동으로 색인을 생성하지만 Cloud Firestore 가이드 에 설명된 대로 맞춤 색인 및 색인 재정의를 정의할 수 있습니다.

Firebase 콘솔이나 CLI를 사용하여 커스텀 색인을 생성, 수정, 배포할 수 있습니다. CLI에서 기본 파일 이름이 firestore.indexes.json 인 인덱스 구성 파일을 편집하고 firebase deploy 명령어를 사용하여 배포합니다.

firebase firestore:indexes 사용하여 CLI로 색인을 내보낼 수 있습니다.

인덱스 구성 파일은 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 문서 필드는 한 가지 모드에서만 색인을 생성할 수 있으므로 필드 객체는 orderarrayConfig 속성을 모두 포함할 수 없습니다.

  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 배열에 있는 한 개체의 스키마는 다음과 같습니다. 선택적 속성은 ? 로 식별됩니다. 성격.

Cloud Firestore 문서 필드는 한 가지 모드에서만 색인을 생성할 수 있으므로 필드 객체는 orderarrayConfig 속성을 모두 포함할 수 없습니다.

  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 배열을 사용하여 다음과 같이 TTL 정책을 활성화하거나 비활성화할 수 있습니다.

  // 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" }
      ]
    }
  ]
}

TTL(수명) 정책에 대한 자세한 내용은 공식 문서를 검토하세요.