使用CreateSearchIndex方法在資料表上建立一個多元索引。一個資料表支援建立多個多元索引。建立多元索引時,您需要將要查詢的欄位添加到多元索引中,您還可以配置多元索引路由鍵、預排序等進階選項。
前提條件
完成建立資料表,並且資料表同時滿足以下條件:
最大版本數必須為1。
資料生命週期為-1或者資料表為禁止更新狀態。
注意事項
建立多元索引時,多元索引中欄位的資料類型必須與資料表中欄位的資料類型相匹配。
如果要修改多元索引為指定資料生命週期(即取值不為-1),則您必須禁用資料表的UpdateRow更新寫入功能。同時多元索引的TTL值必須小於或等於資料表的TTL值。更多資訊,請參見生命週期管理。
參數
建立多元索引時,需要指定資料表名稱(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(永不到期)。 當資料的儲存時間超過設定的資料生命週期時,系統會自動清理超過資料生命週期的資料。 多元索引生命週期的使用方式,請參見生命週期管理。 |
樣本
建立多元索引時使用預設配置
以下樣本用於建立一個多元索引。該多元索引包含col_keyword(Keyword類型)、col_long(Long類型)和col_vector(Vector類型)三列。
func createSearchIndex(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("col_keyword"),
FieldType: tablestore.FieldType_KEYWORD, // 字串類型
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_long"),
FieldType: tablestore.FieldType_LONG, // 數字類型
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_vector"),
FieldType: tablestore.FieldType_VECTOR, // 向量類型
Index: proto.Bool(true),
VectorOptions: &tablestore.VectorOptions{
VectorDataType: tablestore.VectorDataType_FLOAT_32.Enum(),
Dimension: proto.Int32(4), // 向量維度為4,相似性演算法為點積
VectorMetricType: tablestore.VectorMetricType_DOT_PRODUCT.Enum(),
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("Failed to create searchIndex with error:", err)
return
}
}
建立多元索引時指定IndexSort
以下樣本用於建立一個多元索引,同時指定索引預排序。該多元索引包含col1(Keyword類型)和col2(Long類型)兩列。
func createSearchIndex_withIndexSort(client *tablestore.TableStoreClient){
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>" //設定資料表名稱。
request.IndexName = "<SEARCH_INDEX_NAME>" //設定多元索引名稱。
schemas := []*tablestore.FieldSchema{}
field1 := &tablestore.FieldSchema{
FieldName: proto.String("col1"), //設定欄位名,使用proto.String用於擷取字串指標。
FieldType: tablestore.FieldType_KEYWORD, //設定欄位類型。
Index: proto.Bool(true), //設定開啟索引。
EnableSortAndAgg: proto.Bool(true), //設定開啟排序與統計彙總功能。
}
field2 := &tablestore.FieldSchema{
FieldName: proto.String("col2"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
}
schemas = append(schemas, field1, field2)
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: schemas, //設定多元索引包含的欄位。
IndexSort: &search.Sort{ // 指定索引預排序。先按照col2升序,再按照col1降序排序。
Sorters: []search.Sorter{
&search.FieldSort{
FieldName: "col2",
Order: search.SortOrder_ASC.Enum(),
},
&search.FieldSort{
FieldName: "col1",
Order: search.SortOrder_DESC.Enum(),
},
},
},
}
resp, err := client.CreateSearchIndex(request) //調用client建立多元索引。
if err != nil {
fmt.Println("error :", err)
return
}
fmt.Println("CreateSearchIndex finished, requestId:", resp.ResponseInfo.RequestId)
}
建立多元索引時設定資料生命週期
請確保資料表的更新狀態為禁止。
func createIndexWithTTL(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
schemas := []*tablestore.FieldSchema{}
field1 := &tablestore.FieldSchema{
FieldName: proto.String("col1"), //設定欄位名,使用proto.String用於擷取字串指標。
FieldType: tablestore.FieldType_KEYWORD, //設定欄位類型。
Index: proto.Bool(true), //設定開啟索引。
EnableSortAndAgg: proto.Bool(true), //設定開啟排序與統計彙總功能。
}
field2 := &tablestore.FieldSchema{
FieldName: proto.String("col2"),
FieldType: tablestore.FieldType_LONG,
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
}
schemas = append(schemas, field1, field2)
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: schemas, //設定多元索引包含的欄位。
}
request.TimeToLive = proto.Int32(3600 * 24 * 7) // 設定多元索引TTL為7天到期。
resp, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("error :", err)
return
}
fmt.Println("createIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId)
}
建立多元索引時開啟查詢高亮
以下樣本用於建立一個多元索引。該多元索引包含col_keyword(Keyword類型)、col_long(Long類型)、col_text(Text類型)和col_nested(Nested類型)四列,其中col_nested包括level1_text(Text類型)和level1_nested(Nested類型)兩個子列,level1_nested子列還包含了level2_text(Text類型)一個子列。同時為col_text列、col_nested中的level1_text列、col_nested.level1_nested中的level2_text列開啟查詢高亮功能。
func createSearchIndexwithHighlighting(client *tablestore.TableStoreClient) {
request := &tablestore.CreateSearchIndexRequest{}
request.TableName = "<TABLE_NAME>"
request.IndexName = "<SEARCH_INDEX_NAME>"
request.IndexSchema = &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("col_keyword"),
FieldType: tablestore.FieldType_KEYWORD, // 字串類型。
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{
FieldName: proto.String("col_long"),
FieldType: tablestore.FieldType_LONG, // 數字類型。
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
},
{//為非巢狀型別開啟查詢高亮功能。
FieldName: proto.String("col_text"),
FieldType: tablestore.FieldType_TEXT, // 可分詞字串類型。
Index: proto.Bool(true),
EnableSortAndAgg: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
{//為巢狀型別欄位中的子列開啟查詢高亮功能。
FieldName: proto.String("col_nested"),
FieldType: tablestore.FieldType_NESTED,
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("level1_text"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
{
FieldName: proto.String("level1_nested"),
FieldType: tablestore.FieldType_NESTED,
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("level2_text"),
FieldType: tablestore.FieldType_TEXT,
Index: proto.Bool(true),
EnableHighlighting: proto.Bool(true),
},
},
},
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
fmt.Println("Failed to create searchIndex with error:", err)
return
}
}