The time to live (TTL) is an attribute of search indexes that specifies the retention period of data in search indexes. You can specify the TTL of a search index. When data in the search index is retained for a period of time that exceeds the specified TTL, Tablestore automatically deletes the data to free up storage space and reduce costs.
Prerequisites
A TableStoreClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created. For more information, see Create a data table.
Usage notes
Before you specify the TTL of a search index, you must disable the UpdateRow operation on the data table for which the search index is created. Otherwise, semantic issues may occur.
The specified TTL of a data table takes effect on attribute columns, and the specified TTL of a search index takes effect on the entire rows. If a data table is updated by calling the UpdateRow operation, the values of some fields are deleted and the values of some fields are retained in the data table when Tablestore clears data in the data table. However, the entire rows in the search index that is created for the data table are not deleted. As a result, the data in the data table and search index is inconsistent.
If the data table must be updated, check whether you can call the PutRow operation instead of the UpdateRow operation.
The TTL of a search index can be set to -1 or a positive int32. Unit: seconds. A value of -1 indicates that the data in the search index never expires. The maximum int32 value is equivalent to approximately 68 years.
The TTL of a search index is independent of and must be no greater than that of the data table for which the search index is created. If you need to decrease the TTL of a search index and the data table for which the search index is created, you must change the TTL of the search index before you change the TTL of the data table.
Tablestore automatically deletes expired data from search indexes every day. You can still query expired data in search indexes before the expired data is deleted. Tablestore automatically deletes the expired data in the next cycle.
After you change the TTL of data tables and search indexes, Tablestore automatically deletes the historical expired data from the data tables and search indexes in the next cycle.
Procedure
Prohibit the UpdateRow operation on a data table.
The following sample code provides an example on how to prohibit the UpdateRow operation on a data table:
public static void disableTableUpdate(SyncClient client) { UpdateTableRequest updateTableRequest = new UpdateTableRequest("<TABLE_NAME>"); TableOptions options = new TableOptions(); // Prohibit the UpdateRow operation on a data table to prevent impacts on your business. options.setAllowUpdate(false); updateTableRequest.setTableOptionsForUpdate(options); client.updateTable(updateTableRequest); }
Specify the TTL for the search index.
After the UpdateRow operation on a data table is prohibited, you can specify the TTL for an existing search index or when you create a search index.
Specify the TTL when you create a search index
The following sample code provides an example on how to create a search index with the TTL specified. In this example, the search index consists of the following columns: the Col_Keyword column of the String type and the Col_Long column of the Long type. The TTL of the search index is seven days.
// Use Tablestore SDK for Java V5.12.0 or later. public static void createIndexWithTTL(SyncClient client) { int days = 7; CreateSearchIndexRequest createRequest = new CreateSearchIndexRequest(); // Specify the name of the data table. createRequest.setTableName("<TABLE_NAME>"); // Specify the name of the search index. createRequest.setIndexName("<SEARCH_INDEX_NAME>"); IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( // Specify the names and types of the fields. new FieldSchema("Col_Keyword", FieldType.KEYWORD), new FieldSchema("Col_Long", FieldType.LONG))); createRequest.setIndexSchema(indexSchema); // Specify the TTL for the search index. createRequest.setTimeToLiveInDays(days); // Create the search index. client.createSearchIndex(createRequest); }
Change the TTL of an existing search index
The following sample code provides an example on how to set the TTL of an existing search index to 7 days:
// Use Tablestore SDK for Java V5.12.0 or later. public static void updateIndexWithTTL(SyncClient client) { int days = 7; UpdateSearchIndexRequest updateSearchIndexRequest = new UpdateSearchIndexRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>"); // Change the TTL of a search index. updateSearchIndexRequest.setTimeToLiveInDays(days); client.updateSearchIndex(updateSearchIndexRequest); }
The TTL of a data table is independent of the TTL of the search index that is created for the data table. You can specify the TTL for a data table.
The following sample code provides an example on how to set the TTL of a data table to 7 days:
public static void updateTableTTL(SyncClient client) { int days = 7; UpdateTableRequest updateTableRequest = new UpdateTableRequest("<TABLE_NAME>"); TableOptions options = new TableOptions(); options.setTimeToLiveInDays(days); updateTableRequest.setTableOptionsForUpdate(options); client.updateTable(updateTableRequest); }
FAQ
References
You can call the ListSearchIndex operation to query all search indexes that are created for a data table. For more information, see List search indexes.
You can call the DescribeSearchIndex operation to query the description of a search index. For example, you can query the field information and search index configurations. For more information, see Query the description of a search index.
You can dynamically modify the schema of a search index to add, update, or remove index columns in the search index. For more information, see Dynamically modify the schema of a search index.
You can delete a search index that you no longer require. For more information, see Delete search indexes.