全部產品
Search
文件中心

Tablestore:建立多元索引

更新時間:Apr 28, 2025

使用CreateSearchIndex方法在資料表上建立一個多元索引。一個資料表支援建立多個多元索引。建立多元索引時,您需要將要查詢的欄位添加到多元索引中,您還可以配置多元索引路由鍵、預排序等進階選項。

前提條件

注意事項

  • 建立多元索引時,多元索引中欄位的資料類型必須與資料表中欄位的資料類型相匹配。

  • 如果要修改多元索引為指定資料生命週期(即取值不為-1),則您必須禁用資料表的UpdateRow更新寫入功能。同時多元索引的TTL值必須小於或等於資料表的TTL值。更多資訊,請參見生命週期管理

參數

建立多元索引時,需要指定資料表名稱(TableName)、多元索引名稱(IndexName)和索引的結構資訊(IndexSchema),其中IndexSchema包含FieldSchemas(Index的所有欄位的設定)、IndexSetting(索引設定)和IndexSort(索引預排序設定)。詳細參數說明請參見下表。

參數

說明

TableName

資料表名稱。

IndexName

多元索引名稱。

FieldSchemas

FieldSchema的列表,每個FieldSchema包含如下內容:

  • FieldName(必選):建立多元索引的欄位名,即列名,類型為String。

    多元索引中的欄位可以是主鍵列或屬性列。

  • FieldType(必選):欄位的資料類型,類型為tablestore.FieldType_XXX。

  • Array(可選):是否為數組,類型為Boolean。

    如果設定為true,則表示該列是一個數組,在寫入時,必須按照JSON數組格式寫入,例如["a","b","c"]。

    由於Nested類型是一個數組,當FieldType為Nested類型時,無需設定此參數。

  • Index(可選):是否開啟索引,類型為Boolean。

    預設為true,表示對該列構建倒排索引或者空間索引;如果設定為false,則不會對該列構建索引。

  • Analyzer(可選):分詞器類型。當欄位類型為Text時,可以設定此參數;如果不設定,則預設分詞器類型為單字分詞。

  • EnableSortAndAgg(可選):是否開啟排序與統計彙總功能,類型為Boolean。

    只有EnableSortAndAgg設定為true的欄位才能進行排序

    重要

    Nested類型的欄位不支援開啟排序與統計彙總功能,但是Nested類型內部的子列支援開啟排序與統計彙總功能。

  • Store(可選):是否在多元索引中附加儲存該欄位的值,類型為Boolean。

    開啟後,可以直接從多元索引中讀取該欄位的值,而不必反查資料表,可用於查詢效能最佳化。

  • DateFormats(可選):日期的格式,類型為String。當欄位類型為Date時,必須設定此參數。更多資訊,請參見日期時間類型

  • EnableHighlighting(可選):是否開啟摘要與高亮功能,類型為Boolean。預設值為false,表示不開啟摘要與高亮功能。如果要使用摘要與高亮功能,請設定此參數為true。僅Text類型欄位支援摘要與高亮功能。

  • VectorOptions(可選):向量欄位類型的屬性參數。當欄位類型為Vector時,必須設定此參數。包括如下內容:

    • DataType:向量資料類型。當前僅支援float32。如果有其他類型需求,請提交工單聯絡我們。

    • Dimension:向量維度。向量類型欄位支援的最大維度 (Dimension)為2048。

    • MetricType:向量之間距離度量的演算法,支援歐氏距離(euclidean)、餘弦相似性(cosine)、點積(dot_product)。

      • 歐氏距離(euclidean):多維空間中兩個向量之間的直線距離。出於效能考慮,Table Store中的歐氏距離演算法未進行最後的平方根計算。歐氏距離的評分越大表示兩個向量的相似性越大。

      • 餘弦相似性(cosine):向量空間中兩個向量間夾角的餘弦值。餘弦相似性的評分越高表示兩個向量的相似性越大。常用於文本資料的相似性計算。

      • 點積(dot_product):維度相同的兩個向量的對應座標相乘,然後將結果相加。點積的評分越高標識兩個向量的相似性越大。

      選用距離度量演算法的說明請參見距離度量演算法說明

IndexSetting

索引設定,包含RoutingFields設定。

RoutingFields(可選):自訂路由欄位。可以選擇部分主鍵列作為路由欄位,一般情況下只需要設定一個。如果設定多個路由鍵,系統會將多個路由鍵的值拼接成一個值。

在進行索引資料寫入時,系統會根據路由欄位的值計算索引資料的分布位置,路由欄位的值相同的記錄會被索引到相同的資料分區中。

IndexSort

索引預排序設定,包含Sorters設定。如果不設定,則預設按照主鍵排序。

說明

含有Nested類型的索引不支援IndexSort,沒有預排序。

Sorters(必選):索引的預排序方式,支援按照主鍵排序和欄位值排序。關於排序的更多資訊,請參見排序和翻頁

  • PrimaryKeySort表示按照主鍵排序,包含如下設定:

    Order:排序的順序,可按升序或者降序排序,預設為升序。

  • FieldSort表示按照欄位值排序, 包含如下設定:

    只有建立索引且開啟排序與統計彙總功能的欄位才能進行預排序。

    • FieldName:排序的欄位名。

    • Order:排序的順序,可按照升序或者降序排序,預設為升序。

    • Mode:當欄位存在多個值時的排序方式。

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
    }
}

常見問題

相關文檔