The time to live (TTL) can be configured for search indexes. TTL is an attribute of search indexes that specifies the retention period of data in search indexes. When data in a search index is retained for a period of time that exceeds the TTL value, Tablestore automatically deletes the data to free up storage space and reduce costs.

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialization.
  • A data table is created.

Usage notes

  • 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 due to the following reasons:

    The TTL feature of data tables takes effect on attribute columns, and the TTL feature of search indexes takes effect on the entire rows. If the UpdateRow operation is performed on a data table, when the system clears data in the data table, the values of some fields are deleted and the values of some fields are retained 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, data in the data table and search index is inconsistent.

    If the UpdateRow operation is required, check whether the UpdateRow operation can be changed to the PutRow operation.

  • The TTL value of search indexes can be -1 or a positive int32 in seconds. The value of -1 indicates that data in the search index never expires and the maximum int32 value is equivalent to approximately 68 years.
  • The TTL value of a search index is independent of and must be smaller than or equal to the TTL value of the data table for which the search index is created. If you need to change TTL values of search indexes and data tables for which the search indexes are created to smaller values, you must change the TTL values of the search indexes before you change the TTL values of the data tables.
  • Tablestore automatically deletes expired data from search indexes every day. In some cases, you can still query expired data in search indexes. Tablestore automatically deletes the expired data in the next cycle.
  • After you change the TTL values of data tables and search indexes, the system automatically deletes legacy expired data from the data tables and search indexes in the next cycle.

Procedure

  1. Prohibit the UpdateRow operation on a data table.
    public void disableTableUpdate(SyncClient client) {
        UpdateTableRequest updateTableRequest = new UpdateTableRequest(tableName);
        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 search indexes.

    After the UpdateRow operation on a data table is prohibited, you can specify the TTL of a search index when you create the search index or modify the TTL of existing search indexes.

    • 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);
      }
    • Modify the TTL for an existing search index
      // Use Tablestore SDK for Java V5.12.0 or later. 
      public void updateIndexWithTTL(SyncClient client) {
          int days = 7;
          UpdateSearchIndexRequest updateSearchIndexRequest = new UpdateSearchIndexRequest(tableName, indexName);
          // Modify the TTL for the 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. If you want to use the TTL of a data table, configure the TTL for 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);
    }