Configure the time to live (TTL) of a search index with Tablestore SDK for Java. Tablestore automatically removes index data after its retention period expires.
Prerequisites
Before you begin, ensure that you have:
-
Tablestore SDK for Java 5.12.0 or later installed and an initialized client
-
An existing table with a maximum of one data version
Usage notes
-
Before you use search index lifecycle management, disable
UpdateRowoperations on the table to prevent data inconsistencies between the table and search index.A table TTL applies to individual attribute columns, whereas a search index TTL applies to the entire row. With
UpdateRow, TTL cleanup may remove only some table columns while retaining the entire row in the search index.To update data, use
PutRowto 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 updates for the table, configuring the search index TTL, and optionally configuring the table TTL. Configuring the table TTL is not always the last operation. The operation order must ensure that the search index TTL is 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.
-
Disable table updates with
UpdateRow.The following example disables
UpdateRowupdates forexample_table.String tableName = "example_table"; UpdateTableRequest request = new UpdateTableRequest(tableName); TableOptions tableOptions = new TableOptions(); tableOptions.setAllowUpdate(false); request.setTableOptionsForUpdate(tableOptions); client.updateTable(request); -
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.
String tableName = "example_table"; String indexName = "example_index"; IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("category", FieldType.KEYWORD), new FieldSchema("price", FieldType.LONG))); CreateSearchIndexRequest request = new CreateSearchIndexRequest(); request.setTableName(tableName); request.setIndexName(indexName); request.setIndexSchema(indexSchema); request.setTimeToLiveInDays(7); client.createSearchIndex(request);Existing index
Update the TTL of an existing search index to seven days.
String tableName = "example_table"; String indexName = "example_index"; UpdateSearchIndexRequest request = new UpdateSearchIndexRequest(tableName, indexName); request.setTimeToLiveInDays(7); client.updateSearchIndex(request); -
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 TTL constraint described above.String tableName = "example_table"; UpdateTableRequest request = new UpdateTableRequest(tableName); TableOptions tableOptions = new TableOptions(); tableOptions.setTimeToLiveInDays(7); request.setTableOptionsForUpdate(tableOptions); client.updateTable(request);
After configuration, query table information and search index information to verify both TTLs.