用户之声——提建议·赢好礼> HOT

功能介绍

update() 接口用于对通过主键(Document ID)与 Filter 表达式过滤检索 Document,对 Document 的部分字段进行更新。同时,支持新增字段。
说明:
新增字段,在创建 Collection 时没有为这些字段设置索引,那么新增这些字段时,系统不会自动为其创建索引。

接口约束

不能变更 Document ID 字段,不要求事务完整性。

请求示例

写入向量数据
写入原始文本
集合未配置 Embedding 参数,则直接更新向量数据。如下示例,在集合 book-vector 中,基于 upsert() 插入的向量数据,通过 documentIds 与 filter 表达式,过滤出需更新的 Document,更新其 vectors 字段的向量数据,并更新 page 字段值为 30,新增字段 test_new_field。
import tcvectordb
from tcvectordb.model.document import Document, Filter, SearchParams
from tcvectordb.model.enum import FieldType, IndexType, MetricType, ReadConsistency
from tcvectordb.model.index import Index, VectorIndex, FilterIndex, HNSWParams

#create a database client object
client = tcvectordb.VectorDBClient(url='http://10.0.X.X', username='root', key='eC4bLRy2va******************************', read_consistency=ReadConsistency.EVENTUAL_CONSISTENCY, timeout=30)
# 指定需更新文档所属的数据库
db = client.database('db-test')
# 指定集合
coll = db.collection('book-vector')
#设置需更新的字段,或增加新的字段
update_doc = Document(vector=[0.2123, 0.290, 0.213], page=30, test_new_field="new field value")
# 对满足查询条件的 Document 更新字段
coll.update(data=update_doc, document_ids=['0001','0002','0003'], filter=Filter(Filter.In("bookName",["三国演义", "西游记"])))
# 更新之后,确认字段已更新
doc_list = coll.query(document_ids=['0001','0002'], retrieve_vector=True, limit=3)
# 输出确认结果
for doc in doc_list:
print(doc)
输出如下信息,可看到 vectors 字段与 page 字段值已更新,新增字段 test_new_field 也已生效。
{'id': '0001', 'vector': [0.21230000257492065, 0.23000000417232513, 0.21299999952316284], 'test_new_field': 'new field value', 'author': '罗贯中', 'page': 30, 'bookName': '三国演义'}
{'id': '0002', 'vector': [0.21230000257492065, 0.2199999988079071, 0.21299999952316284], 'author': '吴承恩', 'test_new_field': 'new field value', 'bookName': '西游记', 'page': 30}
实例在创建 Collection 时,已配置 Embedding 模型,通过 upsert() 写入原始文本,则可输入文本信息,通过 Embedding 将数据向量化更新向量数据。如下示例,基于 upsert() 插入的原始文本,使用 update 接口,通过 documentIds 与 filter 表达式过滤数据,更新其 text 字段的文本信息,更新 page 字段值为 30,并新增字段 test_new_field。
import tcvectordb
from tcvectordb.model.enum import FieldType, IndexType, MetricType, EmbeddingModel, ReadConsistency
from tcvectordb.model.index import Index, VectorIndex, FilterIndex, HNSWParams
from tcvectordb.model.document import Document, Filter, SearchParams

#create a database client object
client = tcvectordb.VectorDBClient(url='http://10.0.X.X', username='root', key='eC4bLRy2va******************************', read_consistency=ReadConsistency.EVENTUAL_CONSISTENCY, timeout=30)

db = client.database('db-test')
coll = db.collection('book-emb')

# 设置需更新的字段
update_doc = Document(text="分久必合,合久必分", page=30, test_new_field="new field value")
# 对满足查询条件的 Document 更新字段
coll.update(data=update_doc, document_ids=['0001', '0002','0003'], filter=Filter(Filter.In("bookName",["三国演义", "西游记"])))
# 更新之后,确认字段已更新,注意 limit 指每页返回的 Document 数量,默认为 1。
doc_list = coll.query(document_ids=['0001','0002'], retrieve_vector=False, limit=3)
# 输出确认结果
for doc in doc_list:
print(doc)
输出如下信息,可看到 text 字段与 page 字段值已更新,新增字段 test_new_field 也已生效。
{'id': '0002', 'page': 30, 'bookName': '西游记', 'test_new_field': 'new field value', 'text': '分久必合,合久必分', 'author': '吴承恩'}
{'id': '0001', 'author': '罗贯中', 'text': '分久必合,合久必分', 'page': 30, 'test_new_field': 'new field value', 'bookName': '三国演义'}

请求参数

参数名称
参数含义
子参数
是否必选
配置方法
document_ids
表示要更新的 Document 的 ID。
-
ID 长度限制为[1,128]。
批量删除,数据元素最大值为20。
filter
使用创建 Collection 指定的 Filter 索引的字段设置查询过滤表达式
-
Filter 的表达式格式为 '<field_name><operator><value>',多个表达式之间支持 and(与)、or(或)、not(非)关系。具体信息,请参见 Filter 条件表达式。其中
<field_name>:表示要过滤的字段名。
<operator>:表示要使用的运算符。
string :匹配单个字符串值(=)、排除单个字符串值(!=)、匹配任意一个字符串值(in)、排除所有字符串值(not in)。其对应的 Value 必须使用英文双引号括起来。
unit64:大于(>)、大于等于(>=)、等于(==)、小于(<)、小于等于(<=)。例如:expired_time > 1623388524。
array:数组类型,包含数组元素之一(include)、排除数组元素之一(exclude)、全包含数组元素(include all)。例如,name include (\\"Bob\\", \\"Jack\\")。
<value>:表示要匹配的值。
示例:Filter('author="jerry"').And('page>20')。
data
设置需更新的字段,或新增字段。
vector
更新 vector 字段的向量数据。
说明:
如果 Collection 在创建时,未配置 Embedding 参数,或者该实例并未开通 Embedding 功能,则只能配置该参数,输入向量数据,更新数据。

text
Embedding 模型输入文本的字段名。该字段在创建 Collection 时定义。本示例为 text 。
string:字符型。
float:浮点型数据。
说明:
如果 Collection 在创建时,已配置 Embedding 参数,则只能配置该参数,依据输入的文本信息更新向量数据,并将文本字段与向量数据更新存于数据库。

old_field
当前已存在的字段,更新字段对应的数据。
类型:string。
字符长度要求:[1,256]。
new_field
新增字段,并给新字段赋值。
类型:string。
字符长度要求:[1,256]。
timeout
请求超时时间。
-
单位:秒。
默认值:VectorDBClient() 接口配置的 timeout 时长。
取值范围:大于等于0。