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. You can also configure advanced options, such as routing fields and presorting.
Prerequisites
Initialize a Tablestore client. For more information, see Initialize a Tablestore client.
Create a data table that meets the following conditions. For more information, see Create a data table.
The max versions must be 1.
The time to live (TTL) is -1 or updates on the data table are disabled.
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 time to live (TTL) value other than -1 for a search index, you must 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 using the setter method only when you dynamically modify 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, you must 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 | Index settings, which include 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 you write data to the index, the system uses the routing field values to determine the data distribution. Records with the same routing field values are indexed into the same data partition. |
indexSort | Index presorting settings, which include the sorters setting. If you do not set this, data is sorted by primary key by default. Note The indexSort parameter is not supported for indexes that contain Nested types. No presorting is performed. sorters (Optional): A list of presorting methods for the index. You can sort by primary key or by field value. For more information about sorting, see Sorting and pagination.
|
sourceIndexName | Optional. You do not need to set this parameter in most cases. Set this parameter using the setter method only when you dynamically modify the search index schema. This parameter specifies the name of the source search index for reindexing. |
timeToLive | Optional. The time to live (TTL), which is the data retention period in seconds. The default value is -1, which means the data never expires. The value must be at least 86400 (one day) or -1. If the data retention period exceeds the specified TTL, the system automatically deletes the expired data. For more information about how to use the lifecycle of a search index, see Lifecycle management. |
Examples
Create a search index with default configurations
The following example shows how to create a search index. The index contains three columns: Col_Keyword (KEYWORD type), Col_Long (LONG type), and Col_Vector (VECTOR type). The 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 shows how to create a search index. The index contains four columns: Col_Keyword (KEYWORD type), Col_Long (LONG type), Col_Text (TEXT type), and Timestamp (LONG type). The 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 lifecycle
Make sure that updates to the data table are disabled.
The following example shows how to create a search index. The index contains two columns: Col_Keyword (KEYWORD type) and Col_Long (LONG type). The lifecycle 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 shows how to create 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 you create a search index
The following example shows how to create a search index. The index 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, you can select an appropriate query type to perform 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 perform sorting and pagination, highlighting, or collapse (deduplication) operations on the result set.
After you create a search index, you can manage it as required. 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, you can use the aggregation feature or the SQL query feature.
To quickly export data without preserving the order of the entire result set, you can use the parallel scan feature.