When you create a search index, you can specify one or more primary key columns as routing fields. When index data is written to the search index, Tablestore determines the location in which the index data is distributed based on the values of the routing fields. The rows in which the values of the routing fields are the same are indexed to the same partition.
Procedure
Specify one or more routing fields when you create a search index.
If you specify routing fields when you create a search index, the routing fields are used to locate the index data for read and write operations on the data.
You can dynamically modify the schema of a search index to dynamically modify the routing fields of the search index. For example, you can dynamically modify the schema of a search index to change the default routing field to custom routing fields or change custom routing fields to the default routing field. The default routing field is the partition key. For more information, see Dynamically modify the schema of a search index.
ImportantOnly primary key columns in Tablestore can be specified as routing fields.
When you query data by using the search index, provide the routing fields in the query request.
Tablestore scans only the specified partitions based on the routing fields that you provide in the query request. This reduces the query latency because the partitions scanned by Tablestore are narrowed down. If you specify routing fields for a search index, you must provide the routing fields when you use the search index to query data. Whether you specify routing fields for a search index does not affect the query results when you use the search index to query data. However, if you do not specify routing fields, Tablestore scans irrelevant partitions. This wastes system resources and increases query latency.
ImportantYou can specify one or more values for a routing field. However, you cannot specify the value range of a routing field.
Methods
You can specify routing fields in the Tablestore console, or use the Tablestore CLI or Tablestore SDKs. You can specify routing fields when you create a search index or modify the routing fields for an existing search index. This section provides examples on how to specify routing fields when you create a search index. The following items describe the prerequisites that must be met before you specify routing fields for a search index:
After you create a search index, you can modify the schema of the search index to modify the routing fields of the search index. For more information, see Dynamically modify the schema of a search index.
A data table for which the max versions is set to 1 is created. The time to live (TTL) of the data table meets one of the following conditions. For more information, see Operations on a data table.
The TTL of the data table is set to -1, which specifies that data in the data table never expires.
The TTL of the data table is set to a value other than -1, and update operations on the data table are prohibited.
If you want to use Tablestore SDKs to specify routing fields, an OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
If you want to use the Tablestore CLI to specify routing fields, the Tablestore CLI is installed and started, and information about the instance that you want to access is configured. For more information, see Download the Tablestore CLI and Start the Tablestore CLI and configure access information.