All Products
Search
Document Center

Tablestore:Specify the TTL of a search index

Last Updated:Jan 24, 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. 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.

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 the Allow Updates parameter to No and click OK.fig_tableupdate

  2. Specify the TTL of a search index.

    After the UpdateRow operation is disabled on a data table, you can specify the TTL of a search index when you create the search index, or change the TTL of an existing 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, and then click OK.fig_ttlcreate

    • Change the TTL of an existing search index

      1. On the Indexes tab of the data table, find the search index for which you want to change 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 Time to Live parameter, and then click Modify.fig_modifyttl

      3. Click OK.

  3. The TTL of a search index is independent of that of the data table for which the search index is created. If you want to use the TTL feature for a data table, specify the TTL of 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.

To specify the TTL of a search index, perform the following steps:

  1. Disable the UpdateRow operation on a data table.

    public void disableTableUpdate(SyncClient client) {
        UpdateTableRequest updateTableRequest = new UpdateTableRequest(tableName);
        TableOptions options = new TableOptions();
        // Disable the UpdateRow operation on a data table to ensure business continuity. 
        options.setAllowUpdate(false);
        updateTableRequest.setTableOptionsForUpdate(options);
        client.updateTable(updateTableRequest);
    }
  2. Specify the TTL of a search index.

    After the UpdateRow operation is disabled on a data table, you can specify the TTL of a search index when you create the search index, or change the TTL of an existing search index.

    • Specify the TTL when you create a search index

      // Use Tablestore SDK for Java V5.12.0 or later to create a search index. 
      public void createIndexWithTTL(SyncClient client) {
          int days = 7;
          CreateSearchIndexRequest createRequest = new CreateSearchIndexRequest();
          createRequest.setTableName(tableName);
          createRequest.setIndexName(indexName);
          createRequest.setIndexSchema(indexSchema);
          // Specify the TTL for the search index. 
          createRequest.setTimeToLiveInDays(days);
          client.createSearchIndex(createRequest);
      }
    • Change the TTL of an existing search index

      // Use Tablestore SDK for Java V5.12.0 or later to create a search index. 
      public void updateIndexWithTTL(SyncClient client) {
          int days = 7;
          UpdateSearchIndexRequest updateSearchIndexRequest = new UpdateSearchIndexRequest(tableName, indexName);
          // Change the TTL of a search index. 
          updateSearchIndexRequest.setTimeToLiveInDays(days);
          client.updateSearchIndex(updateSearchIndexRequest);
      }
  3. The TTL of a search index is independent of that of the data table for which the search index is created. If you want to use the TTL feature for a data table, specify the TTL of the data table.

    public void updateTableTTL(SyncClient client) {
        int days = 7;
        UpdateTableRequest updateTableRequest = new UpdateTableRequest(tableName);
        TableOptions options = new TableOptions();
        options.setTimeToLiveInDays(days);
        updateTableRequest.setTableOptionsForUpdate(options);
        client.updateTable(updateTableRequest);
    }