使用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(必选):索引的预排序方式,支持按照主键排序和字段值排序。关于排序的更多信息,请参见排序和翻页。
|
TimeToLive | 可选参数,默认值为-1。数据生命周期(TTL),即数据的保存时间。 当数据的保存时间超过设置的数据生命周期时,系统会自动清理超过数据生命周期的数据。 数据生命周期至少为86400秒(一天)或-1(数据永不过期)。 多元索引生命周期的使用方式,请参见生命周期管理。 |
示例
- 创建多元索引
创建一个多元索引。该多元索引包含col1和col2两列,类型分别设置为字符串(Keyword)和整型(Long)。
func CreateSearchIndex(client *tablestore.TableStoreClient, tableName string, indexName string) { request := &tablestore.CreateSearchIndexRequest{} request.TableName = "exampletable" //设置数据表名称。 request.IndexName = "examplesearchindex" //设置多元索引名称。 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) }
- 创建多元索引时指定IndexSort。
创建一个多元索引,同时指定索引预排序。该多元索引包含col1和col2两列,类型分别设置为字符串(Keyword)和整型(Long)。
func createSearchIndex_withIndexSort(client *tablestore.TableStoreClient){ request := &tablestore.CreateSearchIndexRequest{} request.TableName = "exampletable" //设置数据表名称。 request.IndexName = "examplesearchindex" //设置多元索引名称。 schemas := []*tablestore.FieldSchema{} field1 := &tablestore.FieldSchema{ FieldName: proto.String("col1"), //设置字段名,使用proto.String用于获取字符串指针。 FieldType: tablestore.FieldType_KEYWORD, //设置字段类型。 Index: proto.Bool(true), //设置开启索引。 EnableSortAndAgg: proto.Bool(true), //设置开启排序与统计聚合功能。 } 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, //设置多元索引包含的字段。 IndexSort: &search.Sort{ // 指定索引预排序。先按照col2升序,再按照col1降序排序。 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) //调用client创建多元索引。 if err != nil { fmt.Println("error :", err) return } fmt.Println("CreateSearchIndex finished, requestId:", resp.ResponseInfo.RequestId) }
- 创建多元索引时设置生命周期重要 请确保数据表的更新状态为禁止。
func createIndexWithTTL(client *tablestore.TableStoreClient) { request := &tablestore.CreateSearchIndexRequest{} request.TableName = "TableName" request.IndexName = "IndexName" // 此处省略其它创建索引的参数,请按需设置。 request.TimeToLive = proto.Int32(3600 * 24 * 7) // 设置多元索引TTL为7天过期。 resp, err := client.CreateSearchIndex(request) if err != nil { fmt.Println("error :", err) return } fmt.Println("createIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId) }