使用CreateSearchIndex介面在資料表上建立一個多元索引。一個資料表可以建立多個多元索引。
前提條件
- 已初始化Client。具體操作,請參見初始化。
- 已建立資料表,且資料表的資料生命週期(TimeToLive)必須為-1,最大版本數(MaxVersions)必須為1。
參數
建立多元索引時,需要指定資料表名稱(TableName)、多元索引名稱(IndexName)和索引的結構資訊(IndexSchema),其中IndexSchema包含FieldSchemas(Index的所有欄位的設定)、IndexSetting(索引設定)和IndexSort(索引預排序設定)。詳細參數說明請參見下表。
參數 | 說明 |
---|---|
TableName | 資料表名稱。 |
IndexName | 多元索引名稱。 |
FieldSchemas | FieldSchema的列表,每個FieldSchema包含如下內容:
|
IndexSetting | 索引設定,包含RoutingFields設定。
RoutingFields(可選):自訂路由欄位。可以選擇部分主鍵列作為路由欄位,在進行索引資料寫入時,會根據路由欄位的值計算索引資料的分布位置,路由欄位的值相同的記錄會被索引到相同的資料分區中。 |
IndexSort | 索引預排序設定,包含Sorters設定。如果不設定,則預設按照主鍵排序。
说明 含有Nested類型的索引不支援IndexSort,沒有預排序。
Sorters(必選):索引的預排序方式,支援按照主鍵排序和欄位值排序。關於排序的更多資訊,請參見排序和翻頁。
|
TimeToLive | 選擇性參數,預設值為-1。資料生命週期(TTL),即資料的儲存時間。
當資料的儲存時間超過設定的資料生命週期時,系統會自動清理超過資料生命週期的資料。 資料生命週期至少為86400秒(一天)或-1(資料永不到期)。 |
樣本
- 樣本1
建立一個多元索引。
/// <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); }
- 樣本2
建立多元索引時指定IndexSort。
/// <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); }
- 樣本3
建立一個包含日期列和虛擬列的多元索引。
/// <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); }