Update the time to live (TTL) of a search index by calling the UpdateSearchIndex operation in Tablestore SDK for Python.
Prerequisites
Before you begin, ensure that you have:
An initialized
OTSClientinstance. For more information, see Initialize an OTSClient instanceA search index created for a data table
The
allow_updateparameter set toFalsefor the data table. For more information, see Update the configurations of a table
Usage notes
The search index TTL is independent of the data table TTL but must be less than or equal to it.
To decrease the TTL, change the search index TTL first, then the data table TTL.
Tablestore automatically deletes expired data from search indexes once per day. Expired data remains queryable until the next deletion cycle runs.
After you update the TTL, Tablestore deletes historical expired data in the next daily cycle.
Parameters
| Parameter | Required | Description |
|---|---|---|
table_name | Yes | The name of the data table. |
index_name | Yes | The name of the search index. |
time_to_live | Yes | The TTL of the search index in seconds. Set to -1 so that data never expires, or set to a positive int32 value. The maximum int32 value is equivalent to approximately 68 years. |
Examples
Set the search index TTL to seven days:
import os
from tablestore import OTSClient
from tablestore.metadata import SearchIndexMeta
# Initialize the client with environment variable credentials
client = OTSClient(
end_point=os.environ["OTS_ENDPOINT"],
access_key_id=os.environ["OTS_ACCESS_KEY_ID"],
access_key_secret=os.environ["OTS_ACCESS_KEY_SECRET"],
instance_name=os.environ["OTS_INSTANCE"],
)
TABLE_NAME = "<TABLE_NAME>"
INDEX_NAME = "<SEARCH_INDEX_NAME>"
def update_search_index_ttl(client, table_name, index_name, ttl_seconds):
"""Update the TTL of a search index.
Args:
client: An initialized OTSClient instance.
table_name: The name of the data table.
index_name: The name of the search index.
ttl_seconds: TTL in seconds. Use -1 for no expiration.
"""
# Set fields to None because only the TTL is being updated
index_meta = SearchIndexMeta(fields=None, time_to_live=ttl_seconds)
client.update_search_index(table_name, index_name, index_meta)
print(f"Search index TTL updated to {ttl_seconds} seconds.")
# Set TTL to 7 days (in seconds)
seven_days = 24 * 3600 * 7
update_search_index_ttl(client, TABLE_NAME, INDEX_NAME, seven_days)Verify the update
After updating the TTL, call describe_search_index to confirm the change:
def verify_search_index_ttl(client, table_name, index_name):
"""Print the current TTL of a search index."""
index_meta, sync_stat = client.describe_search_index(table_name, index_name)
print(f"Current TTL: {index_meta.time_to_live} seconds")
verify_search_index_ttl(client, TABLE_NAME, INDEX_NAME)Disable TTL
To disable TTL so that data never expires, set time_to_live to -1:
# Disable TTL (data never expires)
index_meta = SearchIndexMeta(fields=None, time_to_live=-1)
client.update_search_index(TABLE_NAME, INDEX_NAME, index_meta)FAQ
References
Specify the TTL of a search index: Learn about search index TTL concepts and behavior.
Query the description of a search index: Retrieve detailed information about a search index, including fields and schema.
Dynamically modify the schema of a search index: Add, update, or remove index columns, and change routing keys or presorting methods.