创建多元索引

更新时间:
复制为 MD 格式

使用CreateSearchIndex方法在数据表上创建一个多元索引。一个数据表支持创建多个多元索引。创建多元索引时,您需要将要查询的字段添加到多元索引中,您还可以配置多元索引路由键、预排序等高级选项。

前提条件

开始前,请确认已完成以下准备工作:

  • 已初始化表格存储客户端。详情请参见初始化Tablestore Client

  • 已创建数据表,详情请参见创建数据表,且数据表同时满足以下条件:

    • 最大版本数为 1。

    • 数据生命周期(TTL)为 -1,或数据表处于禁止更新状态。

字段类型兼容性

多元索引中每个字段的数据类型必须与数据表中对应字段的数据类型保持一致。

参数

创建多元索引时,需要指定数据表名称(TableName)、多元索引名称(IndexName)和索引结构信息(IndexSchema)。IndexSchema 包含 FieldSchemas(字段设置)、IndexSetting(索引设置)和 IndexSort(预排序设置)。

参数

说明

TableName

数据表名称。

IndexName

多元索引名称。

FieldSchemas

FieldSchema 的列表,每个 FieldSchema 包含以下参数:

  • FieldName(必选):多元索引的字段名,对应列名,类型为 String。字段可以是主键列或属性列。

  • FieldType(必选):字段的数据类型,类型为 FieldType.XXX。

  • Array(可选):是否为数组类型,类型为 Boolean。设置为 true 时,写入数据必须使用JSON数组格式,例如 ["a","b","c"]。Nested 类型本身即为数组,FieldType 为 Nested 时无需设置此参数。

  • Index(可选):是否为该字段开启索引,类型为 Boolean。默认为 true,开启后会为该列构建倒排索引或空间索引。设置为 false 则不构建索引。

  • Analyzer(可选)分词器类型。仅当 FieldType 为 Text 时可设置,默认为单字分词。

  • EnableSortAndAgg(可选):是否为该字段开启排序与统计聚合功能,类型为 Boolean。只有设置为 true 的字段才支持排序。Nested 类型不支持排序与统计聚合,但 Nested 类型内部的子列支持。

  • IsVirtualField(可选):该字段是否为虚拟列,类型为 Boolean,默认为 false。仅使用虚拟列时需设置此参数。

  • SourceFieldNames(可选):数据表中的源列名称,类型为 String。IsVirtualField 为 true 时,必须设置此参数。

  • DateFormats(可选):日期格式,类型为 String。FieldType 为 Date 时,必须设置此参数。更多信息,请参见日期时间类型

IndexSetting

索引设置,包含以下参数:

RoutingFields(可选)自定义路由字段。可以选择部分主键列作为路由字段,一般情况下只需要设置一个。如果设置多个路由键,系统会将多个路由键的值拼接成一个值。

在进行索引数据写入时,系统会根据路由字段的值计算索引数据的分布位置,路由字段的值相同的记录会被索引到相同的数据分区中。

IndexSort

索引的预排序设置。不设置时,默认按主键排序。含有 Nested 类型字段的索引不支持 IndexSort。

Sorters(必选):预排序方式,支持按主键排序和按字段值排序。更多信息,请参见排序和翻页

  • PrimaryKeySort:按主键排序。通过 Order 参数指定升序或降序,默认为升序(DataModel.Search.Sort.SortOrder.ASC)。

  • FieldSort:按字段值排序,仅已建立索引且开启排序与统计聚合功能的字段才能用于预排序。包含以下设置:

    • FieldName:排序字段名。

    • Order:排序顺序,支持升序或降序,默认为升序(DataModel.Search.Sort.SortOrder.ASC)。

    • Mode:字段存在多个值时的排序方式。

TimeToLive

可选参数。数据生命周期(TTL),即数据的保存时间,单位为秒。

默认值为 -1,表示数据永不过期。数据生命周期的取值最低为 86400 秒(一天),也可设置为 -1(永不过期)。

当数据的保存时间超过设置的数据生命周期时,系统会自动清理超过数据生命周期的数据。

示例

创建多元索引时使用默认配置

创建一个包含三个字段的多元索引:Keyword_type_col(Keyword 类型)、Long_type_col(Long 类型)和 Text_type_col(Text 类型),其中 Keyword 和 Long 字段开启排序与统计聚合功能。

/// <summary>
/// 创建多元索引,包含三个属性列:Keyword_type_col、Long_type_col、Text_type_col。
/// 字段类型分别为:Keyword(不分词字符串)、Long(整型)、Text(分词字符串)。
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndex(OTSClient otsClient)
{
    // 设置数据表名称和多元索引名称。
    CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
    List<FieldSchema> FieldSchemas = new List<FieldSchema>() {
        new FieldSchema(Keyword_type_col, FieldType.KEYWORD) {
            index = true,            // 开启索引。
            EnableSortAndAgg = true  // 开启排序与统计聚合功能。
        },
        new FieldSchema(Long_type_col, FieldType.LONG) { index = true, EnableSortAndAgg = true },
        new FieldSchema(Text_type_col, FieldType.TEXT) { index = true }
    };
    request.IndexSchame = new IndexSchema()
    {
        FieldSchemas = FieldSchemas
    };
    // 创建多元索引。
    CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);
    Console.WriteLine("Searchindex is created: " + IndexName);
}

创建多元索引时指定IndexSort

创建一个包含三个字段的多元索引:Keyword_type_col(Keyword 类型)、Long_type_col(Long 类型)和 Text_type_col(Text 类型)。同时配置按 Long_type_col 字段升序进行预排序。

/// <summary>
/// 创建多元索引,包含三个属性列:Keyword_type_col、Long_type_col、Text_type_col。
/// 字段类型分别为:Keyword(不分词字符串)、Long(整型)、Text(分词字符串)。
/// 按 Long_type_col 字段升序进行预排序。
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndexWithIndexSort(OTSClient otsClient)
{
    // 设置数据表名称和多元索引名称。
    CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
    List<FieldSchema> FieldSchemas = new List<FieldSchema>() {
        new FieldSchema(Keyword_type_col, FieldType.KEYWORD) {
            index = true,
            EnableSortAndAgg = true
        },
        new FieldSchema(Long_type_col, FieldType.LONG) { index = true, EnableSortAndAgg = true },
        new FieldSchema(Text_type_col, FieldType.TEXT) { index = true }
    };
    request.IndexSchame = new IndexSchema()
    {
        FieldSchemas = FieldSchemas,
        // 按 Long_type_col 字段升序预排序,该字段必须已建立索引并开启 EnableSortAndAgg。
        IndexSort = new DataModel.Search.Sort.Sort()
        {
            Sorters = new List<DataModel.Search.Sort.ISorter>
            {
                new DataModel.Search.Sort.FieldSort(Long_type_col, DataModel.Search.Sort.SortOrder.ASC)
            }
        }
    };

    CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);
    Console.WriteLine("Searchindex is created: " + IndexName);
}

创建一个包含日期列和虚拟列的多元索引

创建一个包含五个字段的多元索引:pk0(Keyword 类型)、pk1(Long 类型)、date_col(Date 类型)、geo_col(Geo-Point 类型)和 col0_v1(Text 类型)。其中虚拟列 col0_v1 映射到数据表中的源列 col0,结果按 pk1 字段升序预排序。

/// <summary>
/// 创建包含日期列和虚拟列的多元索引。
/// 虚拟列 col0_v1 映射到数据表中的源列 col0。
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndex(OTSClient otsClient)
{
    List<FieldSchema> fieldSchemas = new List<FieldSchema> {
        new FieldSchema("pk0", FieldType.KEYWORD)
        {
            index = true,
            EnableSortAndAgg = true
        },

        new FieldSchema("pk1", FieldType.LONG)
        {
            index = true,
            EnableSortAndAgg = true
        },

        new FieldSchema("date_col", FieldType.DATE)
        {
            index = true,
            DateFormats = new List<string>(){
                "yyyy-MM-dd'T'HH:mm:ss.SSSSSS",
                "yyyy-MM-dd'T'HH:mm:ss.SSS"
            }
        },

        new FieldSchema("geo_col", FieldType.GEO_POINT)
        {
            index = true,
            EnableSortAndAgg = true
        },

        new FieldSchema("col0_v1", FieldType.TEXT)
        {
            index = true,
            Analyzer = Analyzer.Split,
            AnalyzerParameter = new SingleWordAnalyzerParameter(true, true),
            IsVirtualField = true,
            SourceFieldNames = new List<string> { "col0" }  // 映射到源列 col0。
        },
    };

    CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
    request.IndexSchame = new IndexSchema()
    {
        FieldSchemas = fieldSchemas,
        IndexSort = new Sort(new List<ISorter> { new FieldSort("pk1", SortOrder.ASC) })
    };
    request.TimeToLive = -1;  // 数据永不过期。

    otsClient.CreateSearchIndex(request);
}

常见问题

相关文档