使用CreateSearchIndex方法在資料表上建立一個多元索引。一個資料表支援建立多個多元索引。建立多元索引時,您需要將要查詢的欄位添加到多元索引中,您還可以配置多元索引路由鍵、預排序等進階選項。
前提條件
完成建立資料表,並且資料表同時滿足以下條件:
最大版本數必須為1。
資料生命週期為 -1或者資料表為禁止更新狀態。
注意事項
建立多元索引時,多元索引中欄位的資料類型必須與資料表中欄位的資料類型相匹配。
參數
建立多元索引時,需要指定資料表名稱(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(永不到期)。 當資料的儲存時間超過設定的資料生命週期時,系統會自動清理超過資料生命週期的資料。 |
樣本
建立多元索引時使用預設配置
以下樣本用於建立一個多元索引。該多元索引包含Keyword_type_col(Keyword類型)、Long_type_col(Long類型)和Text_type_col(TEXT類型)三列。並且開啟排序與統計彙總功能。
/// <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
};
//調用client建立多元索引。
CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);
Console.WriteLine("Searchindex is created: " + IndexName);
}
建立多元索引時指定IndexSort
以下樣本用於建立一個多元索引,多元索引包含Keyword_type_col、Long_type_col、Text_type_col三列,類型分別設定為字串(Keyword)、整型(Long)、分詞字串(TEXT)。同時配置按照Long_type_col列進行預排序。
/// <summary>
/// 建立一個多元索引,包含Keyword_type_col、Long_type_col、Text_type_col三個屬性列,類型分別設定為不分詞字串(Keyword)、整型(Long)、分詞字串(Text)。
/// </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列進行預排序,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>
/// 建立一個包含日期列和虛擬列的多元索引。
/// </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" }
},
};
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);
}