使用 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),
},
}