You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python库向Vertex AI向量搜索现有索引添加新数据

GCP向量搜索:Python批量增量添加数据及UpdateIndex说明

一、Python客户端实现批量插入新数据

根据你的数据来源,有两种实用方式:

1. 直接批量插入内存中的数据点

如果新数据是实时生成的向量+元数据,直接用google-cloud-vector-search的upsert_data_points接口即可,支持批量插入/更新,示例代码:

from google.cloud import vector_v1
from google.protobuf import field_mask_pb2

# 初始化客户端
client = vector_v1.VectorSearchClient()

# 拼接索引的完整路径
index_path = client.index_path(
    project="你的GCP项目ID",
    location="你的索引部署区域(如us-central1)",
    index="你的索引名称"
)

# 准备批量数据点,可自定义datapoint_id、向量、元数据过滤规则
batch_data = [
    {
        "datapoint_id": "new_item_001",
        "feature_vector": [0.12, 0.34, 0.56, 0.78],  # 替换成你的向量值
        "restricts": [{"namespace": "default", "allow_list": ["tag:tech"]}]
    },
    {
        "datapoint_id": "new_item_002",
        "feature_vector": [0.90, 0.23, 0.45, 0.67],
        "restricts": [{"namespace": "default", "allow_list": ["tag:life"]}]
    }
]

# 构建请求,指定要更新的字段
request = vector_v1.UpsertDataPointsRequest(
    index=index_path,
    data_points=batch_data,
    update_mask=field_mask_pb2.FieldMask(paths=["feature_vector", "restricts"])
)

# 执行批量插入
response = client.upsert_data_points(request)
print(f"批量插入完成,返回结果: {response}")

2. 基于Cloud Storage桶的增量同步

如果你的新数据已经追加到创建索引时使用的GCS存储桶中,不需要全量重建索引,直接触发增量更新即可:

from google.cloud import vector_v1
from google.protobuf import field_mask_pb2

client = vector_v1.VectorSearchClient()

index_path = client.index_path(
    project="你的GCP项目ID",
    location="你的索引部署区域",
    index="你的索引名称"
)

# 构建更新请求,明确指定增量更新方式
request = vector_v1.UpdateIndexRequest(
    index={
        "name": index_path,
        "index_update_method": vector_v1.Index.IndexUpdateMethod.INCREMENTAL_UPDATE
    },
    update_mask=field_mask_pb2.FieldMask(paths=["index_update_method"])
)

# 触发同步
response = client.update_index(request)
print(f"增量更新已触发,同步状态: {response}")

二、UpdateIndex:全量重建 vs 增量更新的区别

GCP Vector Search的update_index行为完全由索引配置和请求参数决定:

  • 增量更新:如果创建索引时设置了index_update_method: INCREMENTAL_UPDATE,且更新请求中同样指定该参数,系统会仅同步存储桶中新增/修改的数据,和Weaviate、Pinecone的增量插入逻辑一致,无需重新构建整个索引。
  • 全量重建:若创建时设置的是FULL_REBUILD,或更新时强制指定该参数,系统会清空原有索引数据,从零开始重新构建,耗时较长,适合数据大规模替换的场景。

你在GUI中看到的“编辑索引”批量更新,本质就是调用了这个update_index接口,选择增量模式时就是执行上述的增量同步操作。

内容的提问来源于stack exchange,提问作者Below_Average

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:25:30