使用CreateSearchIndex介面在資料表上建立一個多元索引。一個資料表可以建立多個多元索引。
前提條件
- 已初始化Client。具體操作,請參見初始化。
- 已建立資料表,且資料表的資料生命週期(timeToLive)必須為-1,最大版本數(maxVersions)必須為1。
參數
建立多元索引時,需要指定資料表名稱(tableName)、多元索引名稱(indexName)和索引的結構資訊(indexSchema),其中indexSchema包含fieldSchemas(Index的所有欄位的設定)、indexSetting(索引設定)、indexSort(索引預排序設定)和timeToLive(資料生命週期)。詳細參數說明請參見下表。
參數 | 說明 |
---|---|
tableName | 資料表名稱。 |
indexName | 多元索引名稱。 |
fieldSchemas | fieldSchema的列表,每個fieldSchema包含如下內容:
|
indexSetting | 索引設定,包含routingFields設定。
routingFields(可選):自訂路由欄位。可以選擇部分主鍵列作為路由欄位,在進行索引資料寫入時,會根據路由欄位的值計算索引資料的分布位置,路由欄位的值相同的記錄會被索引到相同的資料分區中。 |
indexSort | 索引預排序設定,包含sorters設定。如果不設定,則預設按照主鍵排序。
说明 含有Nested類型的索引不支援indexSort,沒有預排序。
sorters(可選):索引的預排序方式,支援按照主鍵排序和欄位值排序。關於排序的更多資訊,請參見排序和翻頁。
|
timeToLive | 選擇性參數,預設值為-1。資料生命週期(TTL),即資料的儲存時間。
當資料的儲存時間超過設定的資料生命週期時,系統會自動清理超過資料生命週期的資料。 資料生命週期至少為86400秒(一天)或-1(資料永不到期)。 多元索引生命週期的使用方式,請參見多元索引生命週期。 |
樣本
- 建立多元索引。
建立一個多元索引,包含Col_Keyword和Col_Long兩列,類型分別設定為字串(String)和整型(Long)。
private static void createSearchIndex(SyncClient client) { CreateSearchIndexRequest request = new CreateSearchIndexRequest(); request.setTableName(tableName); //設定資料表名稱。 request.setIndexName(indexName); //設定多元索引名稱。 IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("Col_Keyword", FieldType.KEYWORD), //設定欄位名和類型。 new FieldSchema("Col_Long", FieldType.LONG))); request.setIndexSchema(indexSchema); client.createSearchIndex(request); //調用client建立多元索引。 }
- 建立多元索引時指定IndexSort。
private static void createSearchIndexWithIndexSort(SyncClient client) { CreateSearchIndexRequest request = new CreateSearchIndexRequest(); request.setTableName(tableName); //設定資料表名稱。 request.setIndexName(indexName); //設定多元索引名稱。 IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("Col_Keyword", FieldType.KEYWORD), new FieldSchema("Col_Long", FieldType.LONG), new FieldSchema("Col_Text", FieldType.TEXT), new FieldSchema("Timestamp", FieldType.LONG))); //設定按照Timestamp列進行預排序。 indexSchema.setIndexSort(new Sort( Arrays.<Sort.Sorter>asList(new FieldSort("Timestamp", SortOrder.ASC)))); request.setIndexSchema(indexSchema); client.createSearchIndex(request); }
- 建立多元索引時設定生命週期
注意 請確保資料表的更新狀態為禁止。
// 請使用5.12.0及以上版本的Java SDK。 public void createIndexWithTTL(SyncClient client) { int days = 7; CreateSearchIndexRequest createRequest = new CreateSearchIndexRequest(); createRequest.setTableName(tableName); createRequest.setIndexName(indexName); createRequest.setIndexSchema(indexSchema); // 設定多元索引TTL。 createRequest.setTimeToLiveInDays(days); client.createSearchIndex(createRequest); }
- 建立多元索引時指定虛擬列。
建立一個多元索引,多元索引包含Col_Keyword和Col_Long兩列,同時建立虛擬列Col_Keyword_Virtual_Long和Col_Long_Virtual_Keyword。Col_Keyword_Virtual_Long映射為資料表中Col_Keyword列,虛擬列Col_Long_Virtual_Keyword映射為資料表中Col_Long列。
private static void createSearchIndex(SyncClient client) { CreateSearchIndexRequest request = new CreateSearchIndexRequest(); request.setTableName(tableName); //設定資料表名稱。 request.setIndexName(indexName); //設定多元索引名稱。 IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("Col_Keyword", FieldType.KEYWORD), //設定欄位名和類型。 new FieldSchema("Col_Keyword_Virtual_Long", FieldType.LONG) //設定欄位名和類型。 .setVirtualField(true) //設定欄位是否為虛擬列。 .setSourceFieldName("Col_Keyword"), //虛擬列對應的資料表中欄位。 new FieldSchema("Col_Long", FieldType.LONG), new FieldSchema("Col_Long_Virtual_Keyword", FieldType.KEYWORD) .setVirtualField(true) .setSourceFieldName("Col_Long"))); request.setIndexSchema(indexSchema); client.createSearchIndex(request); //調用client建立多元索引。 }