全部產品
Search
文件中心

Tablestore:建立多元索引

更新時間:Aug 08, 2026

使用 Tablestore Go SDK 為資料表建立多元索引,並配置索引欄位、預排序、生命週期、虛擬列及摘要與高亮。

前提條件

開始前,完成以下準備工作:

  • 安裝Tablestore Go SDK並初始化用戶端。

  • 建立資料表,並將最大版本數設定為 1。

  • 資料表的資料生命週期為 -1,或已禁止通過 UpdateRow 更新資料。

功能說明

調用 CreateSearchIndex 方法為指定資料表建立多元索引。同一資料表可以建立多個多元索引。請求中需要指定資料表、索引名稱和完整的索引結構,並將需要查詢的列添加到 FieldSchemas。索引欄位的資料類型必須與資料表中對應列的資料類型匹配,支援的類型請參見資料類型

func (client *tablestore.TableStoreClient) CreateSearchIndex(request *tablestore.CreateSearchIndexRequest) (*tablestore.CreateSearchIndexResponse, error)
說明

建立多元索引是非同步作業。請求成功後,等待索引資料同步完成,再使用索引查詢資料。

以下樣本建立包含 Keyword 和 Long 欄位的多元索引。未設定預排序和生命週期時,索引預設按主鍵排序,資料永不到期。

tableName := "example_table"
indexName := "example_index"

request := &tablestore.CreateSearchIndexRequest{
    TableName: tableName,
    IndexName: indexName,
    IndexSchema: &tablestore.IndexSchema{
        FieldSchemas: []*tablestore.FieldSchema{
            {
                FieldName: proto.String("category"),
                FieldType: tablestore.FieldType_KEYWORD,
                Index:     proto.Bool(true),
            },
            {
                FieldName:        proto.String("price"),
                FieldType:        tablestore.FieldType_LONG,
                Index:            proto.Bool(true),
                EnableSortAndAgg: proto.Bool(true),
            },
        },
    },
}

_, err := client.CreateSearchIndex(request)
if err != nil {
    log.Fatal(err)
}

參數說明

CreateSearchIndexRequest 包含以下參數。

名稱

類型

說明

TableName(必選)

string

資料表名稱。

IndexName(必選)

string

多元索引名稱。

IndexSchema(必選)

*tablestore.IndexSchema

索引結構。

SourceIndexName(可選)

*string

動態修改 Schema 時使用的源索引名稱。具體操作請參見動態修改 Schema

TimeToLive(可選)

*int32

索引資料生命週期,單位為秒,預設值為 -1。取值為 -1 或不小於 86400 的 int32 整數,-1 表示資料永不到期。設定非 -1 值時,必須禁止通過 UpdateRow 更新資料,且索引生命週期不能超過資料表生命週期。更多資訊請參見生命週期管理

索引結構

名稱

類型

說明

FieldSchemas(必選)

[]*tablestore.FieldSchema

索引欄位列表。

IndexSetting(可選)

*tablestore.IndexSetting

索引設定。

IndexSort(可選)

*search.Sort

索引預排序配置。不設定且索引不包含 Nested 欄位時,系統預設按主鍵排序。包含 Nested 欄位的索引不支援預排序。

索引欄位

名稱

類型

說明

FieldName(必選)

*string

索引欄位名稱,可以對應主鍵列或屬性列。

FieldType(必選)

tablestore.FieldType

索引欄位的資料類型。

Index(可選)

*bool

是否建立倒排索引或空間索引。非 Nested 和 JSON 欄位未設定時按 true 處理。

IndexOptions(可選)

*tablestore.IndexOptions

Text 欄位的索引內容粒度。通常無需設定。

Analyzer(可選)

*tablestore.Analyzer

Text 欄位的分詞器。不設定時使用單字分詞。

AnalyzerParameter(可選)

interface{}

分詞器參數。設定 Analyzer 時按分詞器類型配置。

EnableSortAndAgg(可選)

*bool

是否啟用排序與統計彙總。Text 和 Nested 欄位不支援,但 Nested 子欄位支援。

EnableHighlighting(可選)

*bool

是否啟用摘要與高亮。僅 Text 欄位支援,預設值為 false。

Store(可選)

*bool

是否在多元索引中儲存原始欄位值。啟用後可通過 ReturnAllFromIndex 返回欄位值。

IsArray(可選)

*bool

欄位是否為數組。數組值必須以 JSON 數組格式寫入,Nested 欄位無需設定。

FieldSchemas(可選)

[]*tablestore.FieldSchema

Nested 或 JSON 欄位的子欄位列表。Nested 或 JSON 欄位必須配置。

IsVirtualField(可選)

*bool

欄位是否為虛擬列,預設值為 false。

SourceFieldNames(可選)

[]string

虛擬列映射的源欄位列表。設定虛擬列時必須配置,當前僅支援一個源欄位。

DateFormats(可選)

[]string

Date 欄位支援的日期格式列表。Date 欄位必須配置。

VectorOptions(可選)

*tablestore.VectorOptions

Vector 欄位的向量配置。Vector 欄位必須配置。

JsonType(可選)

*tablestore.JsonType

JSON 欄位的索引類型,取值為 JsonType_OBJECT 或 JsonType_NESTED。JSON 欄位必須配置。

TextSimilarity(可選)

*tablestore.TextSimilarity

Text 欄位的相似性演算法,取值為 TextSimilarity_BM25 或 TextSimilarity_SHORT_TEXT。

向量配置

名稱

類型

說明

VectorDataType(必選)

*tablestore.VectorDataType

向量資料類型,當前僅支援 VectorDataType_FLOAT_32。

VectorMetricType(必選)

*tablestore.VectorMetricType

距離度量演算法,支援歐氏距離、餘弦相似性和點積。

Dimension(必選)

*int32

向量維度,最大值為 4096。

索引設定

名稱

類型

說明

RoutingFields(可選)

[]string

自訂路由欄位,可以指定一個或多個主鍵列。路由欄位值相同的資料寫入同一索引分割區。

預排序配置

名稱

類型

說明

Sorters(必選)

[]search.Sorter

預排序方式列表,支援 PrimaryKeySort 和 FieldSort。FieldSort 使用的欄位必須啟用排序與統計彙總。更多資訊請參見排序和翻頁

情境樣本

設定索引預排序

indexSort := &search.Sort{Sorters: []search.Sorter{
    &search.FieldSort{
        FieldName: "price",
        Order:     search.SortOrder_ASC.Enum(),
    },
}}

request := &tablestore.CreateSearchIndexRequest{
    TableName: "example_table",
    IndexName: "example_index",
    IndexSchema: &tablestore.IndexSchema{
        FieldSchemas: []*tablestore.FieldSchema{
            {
                FieldName:        proto.String("price"),
                FieldType:        tablestore.FieldType_LONG,
                Index:            proto.Bool(true),
                EnableSortAndAgg: proto.Bool(true),
            },
        },
        IndexSort: indexSort,
    },
}

_, err := client.CreateSearchIndex(request)
if err != nil {
    log.Fatal(err)
}

設定索引生命週期

ttl := int32(7 * 24 * 60 * 60)
request := &tablestore.CreateSearchIndexRequest{
    TableName:  "example_table",
    IndexName:  "example_index",
    TimeToLive: &ttl,
    IndexSchema: &tablestore.IndexSchema{
        FieldSchemas: []*tablestore.FieldSchema{
            {
                FieldName: proto.String("category"),
                FieldType: tablestore.FieldType_KEYWORD,
                Index:     proto.Bool(true),
            },
        },
    },
}

_, err := client.CreateSearchIndex(request)
if err != nil {
    log.Fatal(err)
}

設定分詞

analyzer := tablestore.Analyzer_Split
delimiter := ","
fields := []*tablestore.FieldSchema{
    {
        FieldName: proto.String("tags"),
        FieldType: tablestore.FieldType_TEXT,
        Index:     proto.Bool(true),
        Analyzer:  &analyzer,
        AnalyzerParameter: tablestore.SplitAnalyzerParameter{
            Delimiter: &delimiter,
        },
    },
}

建立向量欄位

fields := []*tablestore.FieldSchema{
    {
        FieldName: proto.String("embedding"),
        FieldType: tablestore.FieldType_VECTOR,
        Index:     proto.Bool(true),
        VectorOptions: &tablestore.VectorOptions{
            VectorDataType:   tablestore.VectorDataType_FLOAT_32.Enum(),
            VectorMetricType: tablestore.VectorMetricType_COSINE.Enum(),
            Dimension:        proto.Int32(4),
        },
    },
}

建立虛擬列

fields := []*tablestore.FieldSchema{
    {
        FieldName: proto.String("price"),
        FieldType: tablestore.FieldType_LONG,
        Index:     proto.Bool(true),
    },
    {
        FieldName:        proto.String("price_text"),
        FieldType:        tablestore.FieldType_KEYWORD,
        Index:            proto.Bool(true),
        IsVirtualField:   proto.Bool(true),
        SourceFieldNames: []string{"price"},
    },
}

啟用摘要與高亮

analyzer := tablestore.Analyzer_SingleWord
fields := []*tablestore.FieldSchema{
    {
        FieldName:          proto.String("description"),
        FieldType:          tablestore.FieldType_TEXT,
        Index:              proto.Bool(true),
        Analyzer:           &analyzer,
        EnableHighlighting: proto.Bool(true),
    },
}