使用CreateSearchIndex方法在資料表上建立一個多元索引。一個資料表支援建立多個多元索引。建立多元索引時,您需要將要查詢的欄位添加到多元索引中,您還可以配置多元索引路由鍵、預排序等進階選項。
前提條件
完成建立資料表,並且資料表同時滿足以下條件:
最大版本數必須為1。
資料生命週期為 -1或者資料表為禁止更新狀態。
注意事項
參數
建立多元索引時,需要指定資料表名稱(tableName)、多元索引名稱(indexName)和索引的結構資訊(schema),其中schema包含fieldSchemas(Index的所有欄位的設定)、indexSetting(索引設定)和indexSort(索引預排序設定)。詳細參數說明請參見下表。
參數 | 說明 |
tableName | 資料表名稱。 |
indexName | 多元索引名稱。 |
fieldSchemas | fieldSchemas的列表,每個fieldSchema包含如下內容:
|
indexSetting | 索引設定,包含routingFields設定。 routingFields(可選):自訂路由欄位。可以選擇部分主鍵列作為路由欄位,一般情況下只需要設定一個。如果設定多個路由鍵,系統會將多個路由鍵的值拼接成一個值。 在進行索引資料寫入時,系統會根據路由欄位的值計算索引資料的分布位置,路由欄位的值相同的記錄會被索引到相同的資料分區中。 |
indexSort | 索引預排序設定,包含sorters設定。如果不設定,則預設按照主鍵排序。 說明 含有Nested類型的索引不支援indexSort,沒有預排序。 sorters(必選):索引的預排序方式,支援按照主鍵排序和欄位值排序。關於排序的更多資訊,請參見排序和翻頁。
|
timeToLive | 選擇性參數。資料生命週期(TTL),即資料的儲存時間,單位為秒。 預設值為 -1,表示資料永不到期。資料生命週期的取值最低為 86400 秒(一天),也可設定為 -1(永不到期)。 當資料的儲存時間超過設定的資料生命週期時,系統會自動清理超過資料生命週期的資料。 |
樣本
建立多元索引時設定分詞
以下樣本用於建立一個多元索引。該多元索引包括pic_id(Keyword類型)、count(Long類型)、time_stamp(Long類型)、pic_description(Text類型)、col_vector(Vector類型)、pos(Geo-point類型)、pic_tag(Nested類型)、date(Date類型)、analyzer_single_word(Text類型)、analyzer_split(Text類型)、analyzer_fuzzy(Text類型)列。其中pic_tag包括sub_tag_name(Keyword類型)和tag_name(Keyword類型)兩列,analyzer_single_word列使用的分詞類型為單字分詞,analyzer_split列使用的分詞類型為分隔字元分詞,analyzer_fuzzy列使用的分詞類型為模糊分詞。
client.createSearchIndex({
tableName: "<TABLE_NAME>", //設定資料表名稱。
indexName: "<INDEX_NAME>", //設定多元索引名稱。
schema: {
fieldSchemas: [
{
fieldName: "pic_id",
fieldType: TableStore.FieldType.KEYWORD, // 設定欄位名和欄位類型。
index: true, // 設定開啟索引。
enableSortAndAgg: true, // 設定開啟排序和統計功能。
store: false,
isAnArray: false
},
{
fieldName: "count",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false
},
{
fieldName: "time_stamp",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
},
{
fieldName: "pic_description",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
},
{
fieldName: "col_vector",
fieldType: TableStore.FieldType.VECTOR,
index: true,
isAnArray: false,
vectorOptions: {
dataType: TableStore.VectorDataType.VD_FLOAT_32,
dimension: 4,
metricType: TableStore.VectorMetricType.VM_COSINE,
}
},
{
fieldName: "pos",
fieldType: TableStore.FieldType.GEO_POINT,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false,
},
{
fieldName: "pic_tag",
fieldType: TableStore.FieldType.NESTED,
index: false,
enableSortAndAgg: false,
store: false,
fieldSchemas: [
{
fieldName: "sub_tag_name",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: false,
},
{
fieldName: "tag_name",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: false,
}
]
},
{
fieldName: "date",
fieldType: TableStore.FieldType.DATE,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false,
dateFormats: ["yyyy-MM-dd'T'HH:mm:ss.SSSSSS"],
},
{
fieldName: "analyzer_single_word",
fieldType: TableStore.FieldType.TEXT,
analyzer: "single_word",
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzerParameter: {
caseSensitive: true,
delimitWord: false,
}
},
{
fieldName: "analyzer_split",
fieldType: TableStore.FieldType.TEXT,
analyzer: "split",
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzerParameter: {
delimiter: ",",
}
},
{
fieldName: "analyzer_fuzzy",
fieldType: TableStore.FieldType.TEXT,
analyzer: "fuzzy",
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzerParameter: {
minChars: 1,
maxChars: 5,
}
},
],
indexSetting: { //索引的配置選項。
"routingFields": ["count", "pic_id"], //只支援將主鍵列設定為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);
});
建立多元索引時開啟摘要與高亮
以下樣本用於在建立多元索引時開啟摘要與高亮。該多元索引包括k(Keyword類型)、t(Text類型)和n(Nested類型)三個欄位,其中n欄位包括nk(Keyword類型)、nl(Long類型)和nt(Text類型)三個子欄位。同時為t欄位和n欄位中的nt子欄位開啟摘要與高亮功能。
client.createSearchIndex({
tableName: "<TABLE_NAME>", //設定資料表名稱。
indexName: "<SEARCH_INDEX_NAME>", //設定多元索引名稱。
schema: {
fieldSchemas: [
{
fieldName: "k",
fieldType: TableStore.FieldType.KEYWORD, // 設定欄位名和欄位類型。
index: true, // 設定開啟索引。
enableSortAndAgg: true, // 設定開啟排序和統計功能。
store: false,
isAnArray: false
},
{
fieldName: "t",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
enableHighlighting: true, //為欄位開啟摘要與高亮功能。
store: true,
isAnArray: false,
},
{
fieldName: "n",
fieldType: TableStore.FieldType.NESTED,
index: false,
enableSortAndAgg: false,
store: false,
fieldSchemas: [
{
fieldName: "nk",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: false,
},
{
fieldName: "nl",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: true,
store: false,
},
{
fieldName: "nt",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
enableHighlighting: true, //為欄位開啟摘要與高亮功能。
store: false,
},
]
},
],
indexSetting: { //索引的配置選項。
"routingFields": ["id"], //只支援將主鍵列設定為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);
});