使用 Python SDK 更新多元索引的数据生命周期。
前提条件
安装Tablestore Python SDK并初始化客户端。
数据表已禁止通过
UpdateRow更新数据,即allow_update为False。有关设置方法,请参见更新表配置。
功能说明
调用 update_search_index 方法更新指定多元索引的数据生命周期。
def update_search_index(
self,
table_name,
index_name,
index_meta,
)
配置生命周期时,注意以下事项:
Python SDK 的
update_search_index方法仅支持更新生命周期,不能修改索引字段、索引设置或预排序设置。数据表和多元索引的生命周期相互独立,多元索引的生命周期不能超过数据表的生命周期。如果需要将多元索引的生命周期调大到超过数据表当前生命周期,请先调大数据表生命周期;如果需要将数据表生命周期调小到低于多元索引当前生命周期,请先调小多元索引生命周期。
Tablestore 每天清理一次多元索引中的过期数据。数据过期后、下次清理前,查询仍可能返回这些数据。缩短生命周期后,系统会在后续清理任务中删除已过期的存量数据。
以下示例将 example_table 中 example_index 的生命周期更新为 7 天,并查询更新后的值。
index_meta = SearchIndexMeta(
fields=None,
time_to_live=7 * 24 * 60 * 60,
)
client.update_search_index(
"example_table",
"example_index",
index_meta,
)
updated_meta, _ = client.describe_search_index(
"example_table",
"example_index",
)
print(updated_meta.time_to_live)
参数说明
update_search_index 方法包含以下参数。
|
名称 |
类型 |
说明 |
|
table_name(必选) |
|
数据表名称。 |
|
index_name(必选) |
|
多元索引名称。 |
|
index_meta(必选) |
|
要更新的多元索引配置。 |
索引配置
index_meta 的类型为 SearchIndexMeta,更新生命周期时使用以下参数。
|
名称 |
类型 |
说明 |
|
fields(必选) |
|
固定设置为 |
|
time_to_live(可选) |
|
索引数据的生命周期,单位为秒,默认值为 |
场景示例
关闭生命周期
将 time_to_live 设置为 -1,使多元索引中的数据永不过期。
index_meta = SearchIndexMeta(fields=None, time_to_live=-1)
client.update_search_index(
"example_table",
"example_index",
index_meta,
)