使用 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,
)