Cloud Firestore 색인 정의 참조

Cloud Firestore는 가장 일반적인 유형의 쿼리를 지원하기 위해 자동으로 색인을 만들지만 Cloud Firestore 가이드의 설명대로 커스텀 색인 및 색인 재정의를 정의할 수도 있습니다.

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

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

색인 구성 파일은 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 Console을 사용하여 색인을 수정하는 경우 로컬 색인 파일도 업데이트해야 합니다. 색인 관리에 대한 자세한 내용은 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: []
    }
  ]

필드에 기본 색인 생성을 유지하고 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 (수명) 정책에 대한 자세한 내용은 공식 문서를 검토하세요.