使用CreateSearchIndex接口在数据表上创建一个多元索引。一个数据表可以创建多个多元索引。
前提条件
- 已初始化Client,详情请参见初始化。
- 已创建数据表,且数据表的数据生命周期(TimeToLive)必须为-1,最大版本数(MaxVersions)必须为1。
参数
创建多元索引时,需要指定数据表名称(TableName)、多元索引名称(IndexName)和索引的结构信息(IndexSchema),其中IndexSchema包含FieldSchemas(Index的所有字段的设置)、IndexSetting(索引设置)和IndexSort(索引预排序设置)。详细参数说明请参见下表。
参数 | 说明 |
---|---|
TableName | 数据表名称。 |
IndexName | 多元索引名称。 |
FieldSchemas | FieldSchema的列表,每个FieldSchema包含如下内容:
|
IndexSetting | 索引设置,包含RoutingFields设置。
RoutingFields(可选):自定义路由字段。可以选择部分主键列作为路由字段,在进行索引数据写入时,会根据路由字段的值计算索引数据的分布位置,路由字段的值相同的记录会被索引到相同的数据分区中。 |
IndexSort | 索引预排序设置,包含Sorters设置。如果不设置,则默认按照主键排序。
说明 含有Nested类型的索引不支持IndexSort,没有预排序。
Sorters(必选):索引的预排序方式,支持按照主键排序和字段值排序,详情请参见排序和翻页。
|
示例
/**
*创建一个多元索引,包含Col_Keyword和Col_Long两列,类型分别设置为字符串(Keyword)和整型(Long)。
*/
func CreateSearchIndex(client *tablestore.TableStoreClient, tableName string, indexName string) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = tableName //设置数据表名称。
request.IndexName = indexName //设置多元索引名称。
schemas := []*tablestore.FieldSchema{}
field1 := &tablestore.FieldSchema{
FieldName: proto.String("Col_Keyword"), //设置字段名,使用proto.String用于获取字符串指针。
FieldType: tablestore.FieldType_KEYWORD, //设置字段类型。
Index: proto.Bool(true), //设置开启索引。
EnableSortAndAgg: proto.Bool(true), //设置开启排序与统计聚合功能。
}
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, //设置多元索引包含的字段。
}
resp, err := client.CreateSearchIndex(request) //调用client创建多元索引。
if err != nil {
fmt.Println("error :", err)
return
}
fmt.Println("CreateSearchIndex finished, requestId:", resp.ResponseInfo.RequestId)
}
在文档使用中是否遇到以下问题
更多建议
匿名提交