Use Tablestore SDK for Python to configure the time to live (TTL) of a search index. Tablestore automatically removes index data after its retention period expires.
Prerequisites
Install the Tablestore SDK for Python and initialize a client.
A table exists and its maximum number of data versions is set to
1.
Usage notes
Before you manage a search index TTL, disable
UpdateRowoperations on the table to prevent data inconsistencies between the table and the search index. A table TTL applies to individual attribute columns, whereas a search index TTL applies to an entire row. WithUpdateRow, TTL cleanup may remove only some table columns while retaining the entire row in the search index. To update data, usePutRowto overwrite the entire row.A search index TTL is measured in seconds and accepts
-1or a positive 32-bit integer. A value of-1stores data permanently. The maximum positive value represents a retention period of approximately 68 years.The search index TTL and table TTL are configured independently, but the search index TTL must be less than or equal to the table TTL. If the target search index TTL is greater than the current table TTL, increase the table TTL first. If you decrease both TTLs, decrease the search index TTL first.
Tablestore removes expired search index data once per day. Until the next cleanup completes, queries may still return expired data that has not been removed. After you update a table or search index TTL, Tablestore removes data that expires because of the change during the next cleanup cycle.
Procedure
Lifecycle management consists of disabling UpdateRow operations for the table, configuring the search index TTL, and optionally configuring the table TTL. Configuring the table TTL is not always the last operation. Determine the order of Steps 2 and 3 based on the TTL constraint described above.
-
Disable
UpdateRowoperations for the table.The following example disables
UpdateRowoperations forexample_table.table_options = TableOptions( time_to_live=None, max_version=None, max_time_deviation=None, allow_update=False, ) client.update_table( "example_table", table_options=table_options, ) -
Configure the search index TTL.
Choose an option based on whether the search index exists.
New index
Set a seven-day TTL when you create a search index.
fields = [ FieldSchema("category", FieldType.KEYWORD, index=True), FieldSchema("price", FieldType.LONG, index=True), ] index_meta = SearchIndexMeta( fields, time_to_live=7 * 24 * 60 * 60, ) client.create_search_index( "example_table", "example_index", index_meta, )Existing index
Update the TTL of an existing search index to seven days.
index_meta = SearchIndexMeta( fields=None, time_to_live=7 * 24 * 60 * 60, ) client.update_search_index( "example_table", "example_index", index_meta, ) -
Configure the table TTL if needed.
The following example sets the TTL of
example_tableto seven days. Run this operation before or after configuring the search index TTL based on the constraint described above.table_options = TableOptions( time_to_live=7 * 24 * 60 * 60, max_version=None, max_time_deviation=None, ) client.update_table( "example_table", table_options=table_options, )
After configuration, query table information and query search index information to verify both TTLs.