All Products
Search
Document Center

Tablestore:Specify the TTL of a search index

Last Updated:Oct 17, 2024

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.

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.

Procedures

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

  1. Disable the UpdateRow operation on a data table.

    1. In the Description section of the Basic Information tab of the data table, click Modify Attributes.

    2. In the Modify Attributes dialog box, set Allow Update to No, select the check box next to the risk message, and then click OK.fig_tableupdate

  2. 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

    1. On the Indexes tab of the data table, click Create Search Index.

    2. In the Create Index dialog box, configure the Index Name, Time to Live, and Schema Generation Type parameters.

    3. Turn on Advanced Settings, configure the Time to Live parameter, and then click OK.

      image

    Specify the TTL for an existing search index

    1. 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.fig_indexdetailsenter

    2. In the Index Details dialog box, click the fig_indexdetail icon, modify the value of the Time to Live parameter, and then click Modify.

      image

  3. 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.

    1. In the Description section of the Basic Information tab of the data table, click Modify Attributes.

    2. In the Modify Attributes dialog box, configure the Time to Live parameter based on your business requirements and click OK.fig_ttltablemodify

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.

  1. 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);
    }
  2. 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);
    }
  3. 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);
    }