前提條件
開始前,請確認已完成以下準備工作:
已初始化Table Store用戶端。詳情請參見初始化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);
}