You can specify the TTL of a search index by using the Tablestore console or Tablestore SDKs. To use the TTL feature of a search index, you must prohibit the UpdateRow operation on the data table for which the search index is created.
Use the Tablestore console
Disable the UpdateRow operation on a data table.
In the Description section of the Basic Information tab of the data table, click Modify Attributes.
In the Modify Attributes dialog box, set Allow Update to No, select the check box next to the risk message, and then click OK.
Specify the TTL of a search index.
After the UpdateRow operation on a data table is prohibited, you can specify the TTL for existing search indexes or when you create a search index.
Specify the TTL when you create a search index
On the Indexes tab of the data table, click Create Search Index.
In the Create Index dialog box, configure the Index Name, Time to Live, and Schema Generation Type parameters.
Turn on Advanced Settings, configure the Time to Live parameter, and then click OK.
Specify the TTL for an existing search index
On the Indexes tab of the data table, find the search index for which you want to specify the TTL and click Index Details in the Actions column.
In the Index Details dialog box, click the icon, modify the value of the Time to Live parameter, and then click Modify.
The TTL of a search index is independent of the TTL of the data table for which the search index is created. If you want to use the TTL of a data table, configure the TTL for the data table.
In the Description section of the Basic Information tab of the data table, click Modify Attributes.
In the Modify Attributes dialog box, configure the Time to Live parameter based on your business requirements and click OK.
Use Tablestore SDKs
You can specify the TTL of a search index by using Tablestore SDK for Java or Tablestore SDK for Go. In this example, Tablestore SDK for Java is used.
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);
}