更新多元索引配置

更新时间:
复制 MD 格式

使用 Python SDK 更新多元索引的数据生命周期。

前提条件

  • 安装Tablestore Python SDK并初始化客户端。

  • 数据表已禁止通过 UpdateRow 更新数据,即 allow_updateFalse。有关设置方法,请参见更新表配置

功能说明

调用 update_search_index 方法更新指定多元索引的数据生命周期。

def update_search_index(
    self,
    table_name,
    index_name,
    index_meta,
)
重要

配置生命周期时,注意以下事项:

  • Python SDK 的 update_search_index 方法仅支持更新生命周期,不能修改索引字段、索引设置或预排序设置。

  • 数据表和多元索引的生命周期相互独立,多元索引的生命周期不能超过数据表的生命周期。如果需要将多元索引的生命周期调大到超过数据表当前生命周期,请先调大数据表生命周期;如果需要将数据表生命周期调小到低于多元索引当前生命周期,请先调小多元索引生命周期。

  • Tablestore 每天清理一次多元索引中的过期数据。数据过期后、下次清理前,查询仍可能返回这些数据。缩短生命周期后,系统会在后续清理任务中删除已过期的存量数据。

以下示例将 example_tableexample_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(必选)

str

数据表名称。

index_name(必选)

str

多元索引名称。

index_meta(必选)

SearchIndexMeta

要更新的多元索引配置。

索引配置

index_meta 的类型为 SearchIndexMeta,更新生命周期时使用以下参数。

名称

类型

说明

fields(必选)

List[FieldSchema]

固定设置为 None。更新请求不会修改索引字段。

time_to_live(可选)

int

索引数据的生命周期,单位为秒,默认值为 -1。取值为 -1 或 int32 范围内的正整数;-1 表示数据永不过期,最大值约为 68 年。

场景示例

关闭生命周期

time_to_live 设置为 -1,使多元索引中的数据永不过期。

index_meta = SearchIndexMeta(fields=None, time_to_live=-1)
client.update_search_index(
    "example_table",
    "example_index",
    index_meta,
)