使用CreateSearchIndex介面在資料表上建立一個多元索引。一個資料表可以建立多個多元索引。
前提條件
- 已初始化Client。具體操作,請參見初始化。
- 已建立資料表,且資料表的資料生命週期(timeToLive)必須為-1,最大版本數(maxVersions)必須為1。
參數
建立多元索引時,需要指定資料表名稱(tableName)、多元索引名稱(indexName)和索引的結構資訊(schema),其中schema包含fieldSchemas(Index的所有欄位的設定)、indexSetting(索引設定)和indexSort(索引預排序設定)。詳細參數說明請參見下表。
參數 | 說明 |
---|---|
tableName | 資料表名稱。 |
indexName | 多元索引名稱。 |
fieldSchemas | fieldSchemas的列表,每個fieldSchema包含如下內容:
|
indexSetting | 索引設定,包含routingFields設定。
routingFields(可選):自訂路由欄位。可以選擇部分主鍵列作為路由欄位,在進行索引資料寫入時,會根據路由欄位的值計算索引資料的分布位置,路由欄位的值相同的記錄會被索引到相同的資料分區中。 |
indexSort | 索引預排序設定,包含sorters設定。如果不設定,則預設按照主鍵排序。
说明 含有Nested類型的索引不支援indexSort,沒有預排序。
sorters(必選):索引的預排序方式,支援按照主鍵排序和欄位值排序。關於排序的更多資訊,請參見排序和翻頁。
|
樣本
/**
*建立一個多元索引,包含Col_Keyword、Col_Long、Col_Text、Col_Nested四列。
*類型分別設定為字串(Keyword)、整型(Long)、分詞字串(Text)以及嵌套文本(Nested)。
*/
client.createSearchIndex({
tableName: TABLE_NAME, //設定資料表名稱。
indexName: INDEX_NAME, //設定多元索引名稱。
schema: {
fieldSchemas: [
{
fieldName: "Col_Keyword",
fieldType: TableStore.FieldType.KEYWORD, //設定欄位名和欄位類型。
index: true, //設定開啟索引。
enableSortAndAgg: true, //設定開啟排序與統計彙總功能。
store: false,
isAnArray: false
},
{
fieldName: "Col_Long",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false
},
{
fieldName: "Col_Text",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzer: "single_word"
},
// {
// fieldName: "Col_Nested",
// fieldType: TableStore.FieldType.NESTED,
// index: false,
// enableSortAndAgg: false,
// store: false,
// fieldSchemas: [ //嵌套欄位中設定子欄位索引。
// {
// fieldName: "Sub_Col_KeyWord",
// fieldType: TableStore.FieldType.KEYWORD,
// index: true,
// enableSortAndAgg: true,
// store: false
// },
// {
// fieldName: "Sub_Col_Long",
// fieldType: TableStore.FieldType.LONG,
// index: true,
// enableSortAndAgg: true,
// store: false
// }
// ]
// }
],
indexSetting: { //索引的配置選項。
"routingFields": ["Pk_Keyword"], //只支援將主鍵列設定為routingFields。
"routingPartitionSize": null
},
indexSort: {//含有Nested類型的索引不支援indexSort,沒有預排序。
sorters: [
// { //不設定indexSort時,預設為PrimaryKeySort(升序)排序。
// primaryKeySort: {
// order: TableStore.SortOrder.SORT_ORDER_ASC
// }
// },
{
fieldSort: {
fieldName: "Col_Keyword",
order: TableStore.SortOrder.SORT_ORDER_DESC //設定indexSort排序的順序。
}
}
]
},
timeToLive: 1000000, //單位為秒。
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:',data);
});