Use the CreateSearchIndex method to create a search index for a data table. A data table supports multiple search indexes. When you create a search index, add the fields that you want to query to the index. Configure advanced options such as routing fields and presorting as needed.
Prerequisites
-
A Tablestore client is initialized. For more information, see Initialize a Tablestore client.
-
A data table is created and meets the following conditions. For more information, see Create a data table.
-
The max versions parameter is set to 1.
-
The time to live (TTL) of the data table is set to -1, or the UpdateRow operation is disabled for the data table.
-
Usage notes
-
The data types of the fields in a search index must match the data types of the fields in the data table. For more information, see Data types.
-
To set a TTL value other than -1 for a search index, disable the UpdateRow operation for the data table. The TTL value of the search index must be less than or equal to the TTL value of the data table. For more information, see Lifecycle management.
API
public class CreateSearchIndexRequest implements Request {
/** The name of the data table. */
private String tableName;
/** The name of the search index. */
private String indexName;
/** The schema of the search index. */
private IndexSchema indexSchema;
/**
* You do not need to set this parameter in most cases.
* Set this parameter by using the setter method only when dynamically modifying the search index schema. This parameter specifies the name of the source search index for reindexing.
*/
private String sourceIndexName;
/** The TTL for index data, in seconds. After you create the search index, you can call the UpdateSearchIndex operation to dynamically change this parameter. */
private Integer timeToLive;
}
public class IndexSchema implements Jsonizable {
/** The settings of the index. */
private IndexSetting indexSetting;
/** The settings for all fields in the index. */
private List<FieldSchema> fieldSchemas;
/** The custom presorting method for the index. */
private Sort indexSort;
}
Parameters
When you create a search index, specify the data table name (tableName), search index name (indexName), and index schema (indexSchema). The indexSchema includes field schemas (fieldSchemas), index settings (indexSetting), and index presorting settings (indexSort). The following table describes the parameters.
|
Parameter |
Description |
|
tableName |
The name of the data table. |
|
indexName |
The name of the search index. |
|
fieldSchemas |
A list of index fields. Each FieldSchema contains the following parameters:
|
|
indexSetting |
The index settings, including the routingFields setting. routingFields (Optional): The custom routing fields. You can select some primary key columns as routing fields. In most cases, you only need to set one. If you set multiple routing keys, the system concatenates their values into a single value. When index data is written, the system calculates the data distribution based on the values of the routing fields. Records with the same routing field values are stored in the same data partition. |
|
indexSort |
The index presorting settings, including the sorters setting. If not specified, data is sorted by primary key by default. Note
Indexes that contain Nested fields do not support the indexSort parameter. No presorting is performed. sorters (Optional): A list of presorting methods for the index. Supports sorting by primary key or by field value. For more information about sorting, see Sorting and pagination.
|
|
sourceIndexName |
Optional. Not required in most cases. Set this parameter by using the setter method only when dynamically modifying the search index schema. This parameter specifies the name of the source search index for reindexing. |
|
timeToLive |
Optional. The time-to-live (TTL) of data in the search index, which is the data retention period. Unit: seconds. Default value: -1, which indicates that data never expires. Minimum value: 86400 seconds (one day). A value of -1 means data never expires. When the data retention period exceeds the TTL, the system automatically cleans up expired data. For more information about search index lifecycle management, see Lifecycle management. |
Examples
Create a search index with default configurations
The following example creates a search index that contains three columns: Col_Keyword (KEYWORD type), Col_Long (LONG type), and Col_Vector (VECTOR type). Data is presorted by the primary key of the data table and never expires.
private static void createSearchIndex(SyncClient client) {
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
// Set the data table name.
request.setTableName("<TABLE_NAME>");
// Set the search index name.
request.setIndexName("<SEARCH_INDEX_NAME>");
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
// Set the field name and type.
new FieldSchema("Col_Keyword", FieldType.KEYWORD),
new FieldSchema("Col_Long", FieldType.LONG),
// Set the vector type.
new FieldSchema("Col_Vector", FieldType.VECTOR).setIndex(true)
// The vector dimension is 4, and the similarity algorithm is dot product.
.setVectorOptions(new VectorOptions(VectorDataType.FLOAT_32, 4, VectorMetricType.DOT_PRODUCT))
));
request.setIndexSchema(indexSchema);
// Call the client to create the search index.
client.createSearchIndex(request);
}
Create a search index and specify IndexSort
The following example creates a search index that contains four columns: Col_Keyword (KEYWORD type), Col_Long (LONG type), Col_Text (TEXT type), and Timestamp (LONG type). Data is presorted by the Timestamp column.
private static void createSearchIndexWithIndexSort(SyncClient client) {
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
// Set the data table name.
request.setTableName("<TABLE_NAME>");
// Set the search index name.
request.setIndexName("<SEARCH_INDEX_NAME>");
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("Col_Keyword", FieldType.KEYWORD),
new FieldSchema("Col_Long", FieldType.LONG),
new FieldSchema("Col_Text", FieldType.TEXT),
new FieldSchema("Timestamp", FieldType.LONG)
.setEnableSortAndAgg(true)));
// Set presorting by the Timestamp column.
indexSchema.setIndexSort(new Sort(
Arrays.<Sort.Sorter>asList(new FieldSort("Timestamp", SortOrder.ASC))));
request.setIndexSchema(indexSchema);
// Call the client to create the search index.
client.createSearchIndex(request);
}
Create a search index and set the TTL
Make sure that the UpdateRow operation is disabled for the data table.
The following example creates a search index that contains two columns: Col_Keyword (KEYWORD type) and Col_Long (LONG type). The TTL of the search index is set to 7 days.
// Use Tablestore SDK for Java 5.12.0 or later.
public static void createIndexWithTTL(SyncClient client) {
int days = 7;
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
// Set the data table name.
request.setTableName("<TABLE_NAME>");
// Set the search index name.
request.setIndexName("<SEARCH_INDEX_NAME>");
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
// Set the field name and type.
new FieldSchema("Col_Keyword", FieldType.KEYWORD),
new FieldSchema("Col_Long", FieldType.LONG)));
request.setIndexSchema(indexSchema);
// Set the TTL for the search index.
request.setTimeToLiveInDays(days);
// Call the client to create the search index.
client.createSearchIndex(request);
}
Create a search index and specify virtual columns
The following example creates a search index that contains two columns: Col_Keyword (KEYWORD type) and Col_Long (LONG type). The index also includes two virtual columns: Col_Keyword_Virtual_Long (LONG type), which is mapped to the Col_Keyword column in the data table, and Col_Long_Virtual_Keyword (KEYWORD type), which is mapped to the Col_Long column in the data table.
private static void createSearchIndex(SyncClient client) {
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
// Set the data table name.
request.setTableName("<TABLE_NAME>");
// Set the search index name.
request.setIndexName("<SEARCH_INDEX_NAME>");
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
// Set the field name and type.
new FieldSchema("Col_Keyword", FieldType.KEYWORD),
// Set the field name and type.
new FieldSchema("Col_Keyword_Virtual_Long", FieldType.LONG)
// Specify whether the field is a virtual column.
.setVirtualField(true)
// The corresponding field in the data table for the virtual column.
.setSourceFieldName("Col_Keyword"),
new FieldSchema("Col_Long", FieldType.LONG),
new FieldSchema("Col_Long_Virtual_Keyword", FieldType.KEYWORD)
.setVirtualField(true)
.setSourceFieldName("Col_Long")));
request.setIndexSchema(indexSchema);
// Call the client to create the search index.
client.createSearchIndex(request);
}
Enable summary and highlighting when creating a search index
The following example creates a search index that contains three columns: Col_Keyword (KEYWORD type), Col_Long (LONG type), and Col_Text (TEXT type). The summary and highlighting feature is enabled for the Col_Text column.
private static void createSearchIndexWithHighlighting(SyncClient client) {
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
// Set the data table name.
request.setTableName("<TABLE_NAME>");
// Set the search index name.
request.setIndexName("<SEARCH_INDEX_NAME>");
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
// Set the field name and type.
new FieldSchema("Col_Keyword", FieldType.KEYWORD),
new FieldSchema("Col_Long", FieldType.LONG),
// Enable the summary and highlighting feature for the field.
new FieldSchema("Col_Text", FieldType.TEXT).setIndex(true).setEnableHighlighting(true)
));
request.setIndexSchema(indexSchema);
// Call the client to create the search index.
client.createSearchIndex(request);
}
FAQ
-
Differences between range queries using the GetRange and Search operations
-
Data cannot be found using the Search operation of a search index
-
Does Tablestore support queries similar to IN and BETWEEN...AND in relational databases?
-
The "field:xx must enable enable_sort_and_agg" error occurs when you use a search index
References
-
After you create a search index, select an appropriate query type for multi-dimensional data queries. Search index query types include Term query, Terms query, Match all query, Match query, Match phrase query, Prefix query, Suffix query, Range query, Wildcard query, Geo-query, Boolean query, Vector search, Nested query, and Exists query.
-
When you query data, you can apply sorting and pagination, highlighting, or collapse (deduplication) operations on the result set.
-
After you create a search index, manage it as needed. Management operations include dynamically modifying a schema, Lifecycle management, listing search indexes, querying search index descriptions, and deleting a search index.
-
To perform data analytics, such as finding the maximum or minimum value, calculating a sum, or counting rows, use the aggregation feature or the SQL query feature.
-
To quickly export data without preserving the order of the entire result set, use the parallel scan feature.