使用CreateSearchIndex方法在数据表上创建一个多元索引。一个数据表支持创建多个多元索引。创建多元索引时,您需要将要查询的字段添加到多元索引中,您还可以配置多元索引路由键、预排序等高级选项。
前提条件
- 完成创建数据表,并且数据表同时满足以下条件: - 最大版本数必须为1。 
- 数据生命周期为-1或者数据表为禁止更新状态。 
 
注意事项
- 创建多元索引时,多元索引中字段的数据类型必须与数据表中字段的数据类型相匹配。 
- 如果要修改多元索引为指定数据生命周期(即取值不为-1),则您必须禁用数据表的UpdateRow更新写入功能。同时多元索引的TTL值必须小于或等于数据表的TTL值。更多信息,请参见生命周期管理。 
参数
创建多元索引时,需要指定数据表名称(TableName)、多元索引名称(IndexName)和索引的结构信息(IndexSchema),其中IndexSchema包含FieldSchemas(Index的所有字段的设置)、IndexSetting(索引设置)和IndexSort(索引预排序设置)。详细参数说明请参见下表。
| 参数 | 说明 | 
| TableName | 数据表名称。 | 
| IndexName | 多元索引名称。 | 
| FieldSchemas | FieldSchema的列表,每个FieldSchema包含如下内容: 
 | 
| IndexSetting | 索引设置,包含RoutingFields设置。 RoutingFields(可选):自定义路由字段。可以选择部分主键列作为路由字段,一般情况下只需要设置一个。如果设置多个路由键,系统会将多个路由键的值拼接成一个值。 在进行索引数据写入时,系统会根据路由字段的值计算索引数据的分布位置,路由字段的值相同的记录会被索引到相同的数据分区中。 | 
| IndexSort | 索引预排序设置,包含Sorters设置。如果不设置,则默认按照主键排序。 说明  含有Nested类型的索引不支持IndexSort,没有预排序。 Sorters(必选):索引的预排序方式,支持按照主键排序和字段值排序。关于排序的更多信息,请参见排序和翻页。 
 | 
| TimeToLive | 可选参数。数据生命周期(TTL),即数据的保存时间,单位为秒。 默认值为 -1,表示数据永不过期。数据生命周期的取值最低为 86400 秒(一天),也可设置为 -1(永不过期)。 当数据的保存时间超过设置的数据生命周期时,系统会自动清理超过数据生命周期的数据。 多元索引生命周期的使用方式,请参见生命周期管理。 | 
示例
创建多元索引时使用默认配置
以下示例用于创建一个多元索引。该多元索引包含col_keyword(Keyword类型)、col_long(Long类型)和col_vector(Vector类型)三列。
func createSearchIndex(client *tablestore.TableStoreClient) {
    request := &tablestore.CreateSearchIndexRequest{}
    request.TableName = "<TABLE_NAME>"
    request.IndexName = "<SEARCH_INDEX_NAME>"
    request.IndexSchema = &tablestore.IndexSchema{
        FieldSchemas: []*tablestore.FieldSchema{
            {
                FieldName:        proto.String("col_keyword"),
                FieldType:        tablestore.FieldType_KEYWORD, // 字符串类型
                Index:            proto.Bool(true),
                EnableSortAndAgg: proto.Bool(true),
            },
            {
                FieldName:        proto.String("col_long"),
                FieldType:        tablestore.FieldType_LONG, // 数字类型
                Index:            proto.Bool(true),
                EnableSortAndAgg: proto.Bool(true),
            },
            {
                FieldName: proto.String("col_vector"),
                FieldType: tablestore.FieldType_VECTOR, // 向量类型
                Index:     proto.Bool(true),
                VectorOptions: &tablestore.VectorOptions{
                    VectorDataType:   tablestore.VectorDataType_FLOAT_32.Enum(),
                    Dimension:        proto.Int32(4), // 向量维度为4,相似度算法为点积
                    VectorMetricType: tablestore.VectorMetricType_DOT_PRODUCT.Enum(),
                },
            },
        },
    }
    _, err := client.CreateSearchIndex(request)
    if err != nil {
        fmt.Println("Failed to create searchIndex with error:", err)
        return
    }
}创建多元索引时指定IndexSort
以下示例用于创建一个多元索引,同时指定索引预排序。该多元索引包含col1(Keyword类型)和col2(Long类型)两列。
func createSearchIndex_withIndexSort(client *tablestore.TableStoreClient){
    request := &tablestore.CreateSearchIndexRequest{}
    request.TableName = "<TABLE_NAME>" //设置数据表名称。
    request.IndexName = "<SEARCH_INDEX_NAME>" //设置多元索引名称。
    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 = "<TABLE_NAME>"
    request.IndexName = "<SEARCH_INDEX_NAME>"
    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, //设置多元索引包含的字段。
    }
    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)
}创建多元索引时开启查询高亮
以下示例用于创建一个多元索引。该多元索引包含col_keyword(Keyword类型)、col_long(Long类型)、col_text(Text类型)和col_nested(Nested类型)四列,其中col_nested包括level1_text(Text类型)和level1_nested(Nested类型)两个子列,level1_nested子列还包含了level2_text(Text类型)一个子列。同时为col_text列、col_nested中的level1_text列、col_nested.level1_nested中的level2_text列开启查询高亮功能。
func createSearchIndexwithHighlighting(client *tablestore.TableStoreClient) {
    request := &tablestore.CreateSearchIndexRequest{}
    request.TableName = "<TABLE_NAME>"
    request.IndexName = "<SEARCH_INDEX_NAME>"
    request.IndexSchema = &tablestore.IndexSchema{
        FieldSchemas: []*tablestore.FieldSchema{
            {
                FieldName:        proto.String("col_keyword"),
                FieldType:        tablestore.FieldType_KEYWORD, // 字符串类型。
                Index:            proto.Bool(true),
                EnableSortAndAgg: proto.Bool(true),
            },
            {
                FieldName:        proto.String("col_long"),
                FieldType:        tablestore.FieldType_LONG, // 数字类型。
                Index:            proto.Bool(true),
                EnableSortAndAgg: proto.Bool(true),
            },
            {//为非嵌套类型开启查询高亮功能。
                FieldName: proto.String("col_text"),
                FieldType: tablestore.FieldType_TEXT, // 可分词字符串类型。
                Index:     proto.Bool(true),
                EnableSortAndAgg: proto.Bool(true),
                EnableHighlighting: proto.Bool(true),
            },
            {//为嵌套类型字段中的子列开启查询高亮功能。
                FieldName: proto.String("col_nested"),
                FieldType: tablestore.FieldType_NESTED,
                FieldSchemas: []*tablestore.FieldSchema{
                    {
                        FieldName:          proto.String("level1_text"),
                        FieldType:          tablestore.FieldType_TEXT,
                        Index:              proto.Bool(true),
                        EnableHighlighting: proto.Bool(true),
                    },
                    {
                        FieldName: proto.String("level1_nested"),
                        FieldType: tablestore.FieldType_NESTED,
                        FieldSchemas: []*tablestore.FieldSchema{
                            {
                                FieldName:          proto.String("level2_text"),
                                FieldType:          tablestore.FieldType_TEXT,
                                Index:              proto.Bool(true),
                                EnableHighlighting: proto.Bool(true),
                            },
                        },
                    },
                },
            },
        },
    }
    _, err := client.CreateSearchIndex(request)
    if err != nil {
        fmt.Println("Failed to create searchIndex with error:", err)
        return
    }
}