Loading...
Loading...
Compare original and translation side by side
// V2: Returns mutationId
const result = await env.VECTORIZE_INDEX.insert(vectors);
console.log(result.mutationId); // "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
// Vector inserts/deletes may take a few seconds to be reflected// ❌ V1 (deprecated)
{ returnMetadata: true }
// ✅ V2 (required)
{ returnMetadata: 'all' | 'indexed' | 'none' }wrangler vectorize --deprecated-v1// V2:返回mutationId
const result = await env.VECTORIZE_INDEX.insert(vectors);
console.log(result.mutationId); // "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
// 向量插入/删除可能需要几秒才能生效// ❌ V1(已弃用)
{ returnMetadata: true }
// ✅ V2(必填)
{ returnMetadata: 'all' | 'indexed' | 'none' }wrangler vectorize --deprecated-v1// Get index info to check last mutation processed
const info = await env.VECTORIZE_INDEX.describe();
console.log(info.mutationId); // Last mutation ID
console.log(info.processedUpToMutation); // Last processed timestamp// 获取索引信息以检查最后处理的突变
const info = await env.VECTORIZE_INDEX.describe();
console.log(info.mutationId); // 最后一个突变ID
console.log(info.processedUpToMutation); // 最后处理的时间戳undefinedundefined
**Why**: Metadata indexes MUST exist before vectors are inserted. Vectors added before a metadata index was created won't be filterable on that property.
**原因**:元数据索引必须在插入向量之前存在。在元数据索引创建前添加的向量无法通过该属性进行过滤。undefinedundefinedundefinedundefined{
"name": "my-vectorize-worker",
"main": "src/index.ts",
"compatibility_date": "2025-10-21",
"vectorize": [
{
"binding": "VECTORIZE_INDEX",
"index_name": "my-index"
}
],
"ai": {
"binding": "AI"
}
}{
"name": "my-vectorize-worker",
"main": "src/index.ts",
"compatibility_date": "2025-10-21",
"vectorize": [
{
"binding": "VECTORIZE_INDEX",
"index_name": "my-index"
}
],
"ai": {
"binding": "AI"
}
}export interface Env {
VECTORIZE_INDEX: VectorizeIndex;
AI: Ai;
}
interface VectorizeVector {
id: string;
values: number[] | Float32Array | Float64Array;
namespace?: string;
metadata?: Record<string, string | number | boolean | string[]>;
}
interface VectorizeMatches {
matches: Array<{
id: string;
score: number;
values?: number[];
metadata?: Record<string, any>;
namespace?: string;
}>;
count: number;
}export interface Env {
VECTORIZE_INDEX: VectorizeIndex;
AI: Ai;
}
interface VectorizeVector {
id: string;
values: number[] | Float32Array | Float64Array;
namespace?: string;
metadata?: Record<string, string | number | boolean | string[]>;
}
interface VectorizeMatches {
matches: Array<{
id: string;
score: number;
values?: number[];
metadata?: Record<string, any>;
namespace?: string;
}>;
count: number;
}// Equality (implicit $eq)
{ category: "docs" }
// Not equals
{ status: { $ne: "archived" } }
// In/Not in arrays
{ category: { $in: ["docs", "tutorials"] } }
{ category: { $nin: ["deprecated", "draft"] } }
// Range queries (numbers) - NEW in V2
{ timestamp: { $gte: 1704067200, $lt: 1735689600 } }
// Range queries (strings) - prefix searching
{ url: { $gte: "/docs/workers", $lt: "/docs/workersz" } }
// Nested metadata with dot notation
{ "author.id": "user123" }
// Multiple conditions (implicit AND)
{ category: "docs", language: "en", "metadata.published": true }// 相等匹配(隐式$eq)
{ category: "docs" }
// 不相等
{ status: { $ne: "archived" } }
// 在/不在数组中
{ category: { $in: ["docs", "tutorials"] } }
{ category: { $nin: ["deprecated", "draft"] } }
// 范围查询(数字)- V2新增
{ timestamp: { $gte: 1704067200, $lt: 1735689600 } }
// 范围查询(字符串)- 前缀搜索
{ url: { $gte: "/docs/workers", $lt: "/docs/workersz" } }
// 使用点符号访问嵌套元数据
{ "author.id": "user123" }
// 多条件(隐式AND)
{ category: "docs", language: "en", "metadata.published": true }// Few unique values - efficient filtering
metadata: {
category: "docs", // ~10 categories
language: "en", // ~5 languages
published: true // 2 values (boolean)
}// Many unique values - avoid large range scans
metadata: {
user_id: "uuid-v4...", // Millions of unique values
timestamp_ms: 1704067200123 // Use seconds instead
}// 唯一值数量少 - 过滤效率高
metadata: {
category: "docs", // 约10个分类
language: "en", // 约5种语言
published: true // 2种取值(布尔值)
}// 唯一值数量多 - 避免大范围扫描
metadata: {
user_id: "uuid-v4...", // 数百万个唯一值
timestamp_ms: 1704067200123 // 改用秒级时间戳
}// ❌ INVALID metadata keys
metadata: {
"": "value", // Empty key
"user.name": "John", // Contains dot (reserved for nesting)
"$admin": true, // Starts with $
"key\"with\"quotes": 1 // Contains quotes
}
// ✅ VALID metadata keys
metadata: {
"user_name": "John",
"isAdmin": true,
"nested": { "allowed": true } // Access as "nested.allowed" in filters
}// ❌ 无效的元数据键名
metadata: {
"": "value", // 空键名
"user.name": "John", // 包含点号(保留用于嵌套结构)
"$admin": true, // 以$开头
"key\"with\"quotes": 1 // 包含引号
}
// ✅ 有效的元数据键名
metadata: {
"user_name": "John",
"isAdmin": true,
"nested": { "allowed": true } // 在过滤中使用"nested.allowed"访问
}Problem: Filtering doesn't work on existing vectors
Solution: Delete and re-insert vectors OR create metadata indexes BEFORE inserting问题:现有向量无法被过滤
解决方案:删除并重新插入向量,或在插入向量前创建元数据索引Problem: "Vector dimensions do not match index configuration"
Solution: Ensure embedding model output matches index dimensions:
- Workers AI bge-base: 768
- OpenAI small: 1536
- OpenAI large: 3072问题:"向量维度与索引配置不匹配"
解决方案:确保嵌入向量模型的输出与索引维度匹配:
- Workers AI bge-base:768维度
- OpenAI small:1536维度
- OpenAI large:3072维度Problem: "Invalid metadata key"
Solution: Keys cannot:
- Be empty
- Contain . (dot)
- Contain " (quote)
- Start with $ (dollar sign)问题:"无效的元数据键名"
解决方案:键名不能:
- 为空
- 包含.(点号)
- 包含"(引号)
- 以$(美元符号)开头Problem: "Filter exceeds 2048 bytes"
Solution: Simplify filter or split into multiple queries问题:"过滤条件超过2048字节"
解决方案:简化过滤条件或拆分为多个查询Problem: Slow queries or reduced accuracy
Solution: Use lower cardinality fields for range queries, or use seconds instead of milliseconds for timestamps问题:查询缓慢或准确性降低
解决方案:使用低基数字段进行范围查询,或对时间戳使用秒级而非毫秒级Problem: Updates not reflecting in index
Solution: Use upsert() to overwrite existing vectors, not insert()问题:更新未在索引中生效
解决方案:使用upsert()覆盖现有向量,而非insert()Problem: "VECTORIZE_INDEX is not defined"
Solution: Add [[vectorize]] binding to wrangler.jsonc问题:"VECTORIZE_INDEX未定义"
解决方案:在wrangler.jsonc中添加[[vectorize]]绑定Problem: Unclear when to use namespace vs metadata filtering
Solution:
- Namespace: Partition key, applied BEFORE metadata filters
- Metadata: Flexible key-value filtering within namespace问题:不清楚何时使用命名空间或元数据过滤
解决方案:
- 命名空间:分区键,在元数据过滤前生效
- 元数据:命名空间内的灵活键值对过滤Problem: Inserted vectors not immediately queryable
Solution: V2 mutations are asynchronous - vectors may take a few seconds to be reflected
- Use mutationId to track mutation status
- Check env.VECTORIZE_INDEX.describe() for processedUpToMutation timestamp问题:插入的向量无法立即查询到
解决方案:V2的突变操作是异步的 - 向量可能需要几秒才能生效
- 使用mutationId跟踪突变状态
- 查看env.VECTORIZE_INDEX.describe()中的processedUpToMutation时间戳Problem: "returnMetadata must be 'all', 'indexed', or 'none'"
Solution: V2 changed returnMetadata from boolean to string enum:
- ❌ V1: { returnMetadata: true }
- ✅ V2: { returnMetadata: 'all' }问题:"returnMetadata必须为'all'、'indexed'或'none'"
解决方案:V2将returnMetadata从布尔值改为字符串枚举:
- ❌ V1:{ returnMetadata: true }
- ✅ V2:{ returnMetadata: 'all' }npm install -g wrangler@latestreturnMetadatamutationIdwrangler vectorize --deprecated-v1npm install -g wrangler@latestreturnMetadatamutationIdwrangler vectorize --deprecated-v1