Cloud Firestore tự động tạo chỉ mục để hỗ trợ các loại truy vấn phổ biến nhất, nhưng cho phép bạn xác định chỉ mục tuỳ chỉnh và ghi đè chỉ mục như mô tả trong hướng dẫn về Cloud Firestore.
Bạn có thể tạo, sửa đổi và triển khai chỉ mục tuỳ chỉnh trong bảng điều khiển của Firebase hoặc
sử dụng CLI. Từ CLI, hãy chỉnh sửa tệp cấu hình chỉ mục, với tên tệp mặc định là firestore.indexes.json
và triển khai bằng lệnh firebase
deploy
.
Bạn có thể xuất chỉ mục với CLI bằng cách sử dụng firebase firestore:indexes
.
Tệp cấu hình chỉ mục xác định một đối tượng chứa mảng indexes
và một mảng fieldOverrides
không bắt buộc.
Ví dụ:
{
// 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: []
}
]
}
Triển khai cấu hình chỉ mục
Triển khai cấu hình chỉ mục bằng lệnh firebase deploy
. Nếu bạn chỉ muốn triển khai chỉ mục cho các cơ sở dữ liệu đã định cấu hình trong dự án, hãy thêm cờ --only firestore
. Xem tài liệu tham khảo về các tuỳ chọn cho lệnh này.
Để liệt kê các chỉ mục đã triển khai, hãy chạy lệnh firebase firestore:indexes
. Thêm cờ --database=<databaseID>
để liệt kê các chỉ mục cho một cơ sở dữ liệu khác với cơ sở dữ liệu mặc định của dự án.
Nếu bạn chỉnh sửa các chỉ mục bằng bảng điều khiển của Firebase, hãy nhớ cập nhật tệp chỉ mục cục bộ. Để biết thêm về cách quản lý chỉ mục, hãy xem hướng dẫn về Cloud Firestore.
Định dạng JSON
Chỉ số
Giản đồ cho một đối tượng trong mảng indexes
như sau. Các thuộc tính không bắt buộc được xác định bằng ký tự ?
.
Xin lưu ý rằng hệ thống chỉ có thể lập chỉ mục các trường tài liệu trong Cloud Firestore ở một chế độ, do đó, mỗi đối tượng trường chỉ có thể chứa một trong các thuộc tính order
, arrayConfig
và 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
Giản đồ cho một đối tượng trong mảng fieldOverrides
như sau. Các thuộc tính không bắt buộc được xác định bằng ký tự ?
.
Xin lưu ý rằng các trường tài liệu trong Cloud Firestore chỉ có thể được lập chỉ mục ở một chế độ, do đó, một đối tượng trường không thể chứa cả hai thuộc tính order
và 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
Chính sách TTL
Bạn có thể bật hoặc tắt chính sách TTL bằng mảng fieldOverrides
như sau:
// 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: []
}
]
Cách giữ chế độ lập chỉ mục mặc định trong trường và bật chính sách TTL:
{
"fieldOverrides": [
{
"collectionGroup": "yourCollectionGroup",
"fieldPath": "yourFieldPath",
"ttl": true,
"indexes": [
{ "order": "ASCENDING", "queryScope": "COLLECTION_GROUP" },
{ "order": "DESCENDING", "queryScope": "COLLECTION_GROUP" },
{ "arrayConfig": "CONTAINS", "queryScope": "COLLECTION_GROUP" }
]
}
]
}
Để biết thêm thông tin về chính sách thời gian tồn tại (TTL), hãy xem tài liệu chính thức.