You can call the CreateSearchIndex operation to create one or more search indexes for a table.
Prerequisites
- The Client instance is initialized. For more information, see Initialization.
- A table is created for which TimeToLive is set to -1 and MaxVersions is set to 1.
Parameters
Parameter | Description |
---|---|
TableName | The name of the 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: optional. You use this parameter to customize routing fields. You can specify that part of primary key columns are used as routing fields. Tablestore distributes data that is written to a search index to different partitions based on the specified routing fields. The data with the same routing field values is distributed to the same data partition. |
IndexSort | The presorting settings of the search index, including Sorters. If IndexSort is not
set, data is sorted by primary key.
Note Search indexes that contain Nested fields do not support IndexSort.
Sorters: required. This parameter specifies the presorting method for the search index.
PrimaryKeySort and FieldSort are supported. For more information, see Sorting and paging.
|
Examples
/**
*Create a search index that contains the Col_Keyword and Col_Long columns. Set the type of data in Col_Keyword to Keyword. Set the type of data in Col_Long to Long.
*/
func CreateSearchIndex(client *tablestore.TableStoreClient, tableName string, indexName string) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = tableName // Set the name of the table.
request.IndexName = indexName // Set the name of the search index.
schemas := []*tablestore.FieldSchema{}
field1 := &tablestore.FieldSchema{
FieldName: proto.String("Col_Keyword"), // Set the field name by calling the proto.String method. This method is used to request a string pointer.
FieldType: tablestore.FieldType_KEYWORD, // Set the field type.
Index: proto.Bool(true), // Set Index to true.
EnableSortAndAgg: proto.Bool(true), // Set EnableSortAndAgg to true to enable the sorting and aggregation features.
}
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, // Set the fields 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)
}