You can call the CreateSearchIndex operation to create one or more search indexes for a data table.
Prerequisites
- A Tablestore client is initialized. For more information, see Initialization.
- A data table is created for which the value of the TimeToLive parameter is set to -1 and the value of the MaxVersions parameter is set to 1.
Parameters
Parameter | Description |
---|---|
TableName | The name of the data table. |
IndexName | The name of the search index. |
FieldSchemas | The list of field schemas. You can configure the following parameters for each field schema:
|
IndexSetting | The settings of the search index, including RoutingFields. RoutingFields: This parameter is optional and specifies custom routing fields. You can specify some primary key columns as routing fields. Tablestore distributes data that is written to a search index across different partitions based on the specified routing fields. The data whose routing field values are the same is distributed to the same partition. |
IndexSort | The presorting settings for the search index, including Sorters. If no value is specified for the indexSort parameter, field values are sorted by primary key by default. Note You can skip the presorting settings for the search indexes that contain the Nested field type. Sorters: This parameter is required and specifies the presorting method for the search index. PrimaryKeySort and FieldSort are supported. For more information about sorting, see Sorting and paging.
|
TimeToLive | This parameter is optional and specifies the retention period of data in the search index. Unit: seconds. Default value: -1. If the retention period exceeds the TTL value, Tablestore automatically deletes expired data. The minimum TimeToLive value is 86400, which is equal to one day. A value of -1 indicates that data never expires. For more information about how to manage the TTL of search indexes, see TTL of search indexes. |
Examples
- Create a search index
The following sample code shows how to create a search index. The search index contains the col1 and col2 columns. The type of the col1 column is Keyword, and the type of the col2 column is Long.
func CreateSearchIndex(client *tablestore.TableStoreClient, tableName string, indexName string) { request := &tablestore.CreateSearchIndexRequest{} request.TableName = "exampletable" // Specify the name of the data table. request.IndexName = "examplesearchindex" // Specify the name of the search index that you want to create. schemas := []*tablestore.FieldSchema{} field1 := &tablestore.FieldSchema{ FieldName: proto.String("Col_Keyword"), // Specify the field name by calling the proto.String method. This method is used to request a string pointer. FieldType: tablestore.FieldType_KEYWORD, // Specify the field type. Index: proto.Bool(true), // Set Index to true. EnableSortAndAgg: proto.Bool(true), // Enable sorting and aggregation. } field2 := &tablestore.FieldSchema{ FieldName: proto.String("Col_Long"), FieldType: tablestore.FieldType_LONG, Index: proto.Bool(true), EnableSortAndAgg: proto.Bool(true), } schemas = append(schemas, field1, field2) request.IndexSchema = &tablestore.IndexSchema{ FieldSchemas: schemas, // Specify the fields that are included in the search index. } resp, err := client.CreateSearchIndex(request) // Call a client to create the search index. if err != nil { fmt.Println("error :", err) return } fmt.Println("CreateSearchIndex finished, requestId:", resp.ResponseInfo.RequestId) }
- Create a search index with indexSort specified
The following sample code shows how to create a search index with IndexSort specified. The search index contains the col1 and col2 columns. The type of the col1 column is Keyword, and the type of the col2 column is Long.
func createSearchIndex_withIndexSort(client *tablestore.TableStoreClient){ request := &tablestore.CreateSearchIndexRequest{} request.TableName = "exampletable" // Specify the name of the data table. request.IndexName = "examplesearchindex" // Specify the name of the search index that you want to create. schemas := []*tablestore.FieldSchema{} field1 := &tablestore.FieldSchema{ FieldName: proto.String("col1"), // Set the field name by calling the proto.String method. This method is used to request a string pointer. FieldType: tablestore.FieldType_KEYWORD, // Specify the field type. Index: proto.Bool(true), // Set Index to true. EnableSortAndAgg: proto.Bool(true), // Enable sorting and aggregation. } field2 := &tablestore.FieldSchema{ FieldName: proto.String("col2"), FieldType: tablestore.FieldType_LONG, Index: proto.Bool(true), EnableSortAndAgg: proto.Bool(true), } schemas = append(schemas, field1, field2) request.IndexSchema = &tablestore.IndexSchema{ FieldSchemas: schemas, // Specify the fields that are included in the search index. IndexSort: &search. Sort{ // Specify the index pre-sorting settings. The values in the col2 column are sorted in ascending order, and the values in the col1 column are sorted in descending order. Sorters: []search.Sorter{ &search.FieldSort{ FieldName: "col2", Order: search.SortOrder_ASC.Enum(), }, &search.FieldSort{ FieldName: "col1", Order: search.SortOrder_DESC.Enum(), }, }, }, } resp, err := client.CreateSearchIndex(request) // Call a client to create the search index. if err != nil { fmt.Println("error :", err) return } fmt.Println("CreateSearchIndex finished, requestId:", resp.ResponseInfo.RequestId) }
- Create a search index with the TTL specifiedImportant Make sure that updates to the data table are prohibited.
func createIndexWithTTL(client *tablestore.TableStoreClient) { request := &tablestore.CreateSearchIndexRequest{} request.TableName = "TableName" request.IndexName = "IndexName" // In this example, other parameters are not configured. Configure other parameters based on your business requirements. request.TimeToLive = proto.Int32(3600 * 24 * 7) // Set the TTL of the search index to 7 days. resp, err := client.CreateSearchIndex(request) if err != nil { fmt.Println("error :", err) return } fmt.Println("createIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId) }