前提条件
开始前,请确认已完成以下准备工作:
已初始化表格存储客户端。详情请参见初始化Tablestore Client。
-
已创建数据表,详情请参见创建数据表,且数据表同时满足以下条件:
最大版本数为 1。
数据生命周期(TTL)为 -1,或数据表处于禁止更新状态。
字段类型兼容性
多元索引中每个字段的数据类型必须与数据表中对应字段的数据类型保持一致。
参数
创建多元索引时,需要指定数据表名称(TableName)、多元索引名称(IndexName)和索引结构信息(IndexSchema)。IndexSchema 包含 FieldSchemas(字段设置)、IndexSetting(索引设置)和 IndexSort(预排序设置)。
|
参数 |
说明 |
|
TableName |
数据表名称。 |
|
IndexName |
多元索引名称。 |
|
FieldSchemas |
FieldSchema 的列表,每个 FieldSchema 包含以下参数:
|
|
IndexSetting |
索引设置,包含以下参数: RoutingFields(可选):自定义路由字段。可以选择部分主键列作为路由字段,一般情况下只需要设置一个。如果设置多个路由键,系统会将多个路由键的值拼接成一个值。 在进行索引数据写入时,系统会根据路由字段的值计算索引数据的分布位置,路由字段的值相同的记录会被索引到相同的数据分区中。 |
|
IndexSort |
索引的预排序设置。不设置时,默认按主键排序。含有 Nested 类型字段的索引不支持 IndexSort。 Sorters(必选):预排序方式,支持按主键排序和按字段值排序。更多信息,请参见排序和翻页。
|
|
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);
}