使用CreateSearchIndex接口在数据表上创建一个多元索引。一个数据表可以创建多个多元索引。

前提条件

  • 已初始化Client。具体操作,请参见初始化
  • 已创建数据表,且数据表的数据生命周期(TimeToLive)必须为-1,最大版本数(MaxVersions)必须为1。

参数

创建多元索引时,需要指定数据表名称(TableName)、多元索引名称(IndexName)和索引的结构信息(IndexSchema),其中IndexSchema包含FieldSchemas(Index的所有字段的设置)、IndexSetting(索引设置)和IndexSort(索引预排序设置)。详细参数说明请参见下表。
参数说明
TableName数据表名称。
IndexName多元索引名称。
FieldSchemasFieldSchema的列表,每个FieldSchema包含如下内容:
  • FieldName(必选):创建多元索引的字段名,即列名,类型为String。

    多元索引中的字段可以是主键列或者属性列。

  • FieldType(必选):字段类型,类型为tablestore.FieldType_XXX。更多信息,请参见数据类型映射
  • Array(可选):是否为数组,类型为Boolean。

    如果设置为true,则表示该列是一个数组,在写入时,必须按照JSON数组格式写入,例如["a","b","c"]。

    由于Nested类型是一个数组,当FieldType为Nested类型时,无需设置此参数。

  • Index(可选):是否开启索引,类型为Boolean。

    默认为true,表示对该列构建倒排索引或者空间索引;如果设置为false,则不会对该列构建索引。

  • Analyzer(可选):分词器类型。当字段类型为Text时,可以设置此参数;如果不设置,则默认分词器类型为单字分词。关于分词的更多信息,请参见分词
  • EnableSortAndAgg(可选):是否开启排序与统计聚合功能,类型为Boolean。
    只有EnableSortAndAgg设置为true的字段才能进行排序。关于排序的更多信息,请参见排序和翻页
    重要 Nested类型的字段不支持开启排序与统计聚合功能,但是Nested类型内部的子列支持开启排序与统计聚合功能。
  • Store(可选):是否在多元索引中附加存储该字段的值,类型为Boolean。

    开启后,可以直接从多元索引中读取该字段的值,而不必反查数据表,可用于查询性能优化。

  • DateFormats(可选):日期的格式,类型为String。当字段类型为Date时,必须设置此参数。更多信息,请参见日期数据类型
IndexSetting索引设置,包含RoutingFields设置。

RoutingFields(可选):自定义路由字段。可以选择部分主键列作为路由字段,在进行索引数据写入时,会根据路由字段的值计算索引数据的分布位置,路由字段的值相同的记录会被索引到相同的数据分区中。

IndexSort索引预排序设置,包含Sorters设置。如果不设置,则默认按照主键排序。
说明 含有Nested类型的索引不支持IndexSort,没有预排序。
Sorters(必选):索引的预排序方式,支持按照主键排序和字段值排序。关于排序的更多信息,请参见排序和翻页
  • PrimaryKeySort表示按照主键排序,包含如下设置:

    Order:排序的顺序,可按升序或者降序排序,默认为升序。

  • FieldSort表示按照字段值排序, 包含如下设置:

    只有建立索引且开启排序与统计聚合功能的字段才能进行预排序。

    • FieldName:排序的字段名。
    • Order:排序的顺序,可按照升序或者降序排序,默认为升序。
    • Mode:当字段存在多个值时的排序方式。
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)
    }