All Products
Search
Document Center

Tablestore:Create a search index

Last Updated:Aug 07, 2026

Use the Tablestore SDK for Go to create a search index and configure index fields, presorting, TTL, virtual columns, and highlighting.

Prerequisites

Before you begin, complete the following preparations:

  • Install the Tablestore Go SDK and initialize a client.

  • A data table is created, and its maximum number of versions is set to 1.

  • The data table has a time-to-live (TTL) value of -1, or updates by using UpdateRow are disabled.

Description

Call CreateSearchIndex to create a search index for a data table. You can create multiple search indexes for one data table. Specify the data table, index name, and complete index schema, and add queryable columns to FieldSchemas. The data types of index fields must match those of the corresponding table columns. For supported types, see Data types.

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

Creating a search index is asynchronous. After the request succeeds, wait until index data is synchronized before querying the index.

The following example creates a search index that contains Keyword and Long fields. If presorting and TTL are not configured, the index is sorted by primary key and data does not expire.

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

Parameters

CreateSearchIndexRequest contains the following parameters.

Name

Type

Description

TableName (required)

string

The name of the data table.

IndexName (required)

string

The name of the search index.

IndexSchema (required)

*tablestore.IndexSchema

The index schema.

SourceIndexName (optional)

*string

The source index used to dynamically modify a schema. For more information, see Dynamically modify a search index schema.

TimeToLive (optional)

*int32

The TTL of index data in seconds. Default value: -1. Valid values are -1 and int32 integers no less than 86,400. -1 indicates that data never expires. For a non--1 value, updates by using UpdateRow must be disabled, and the index TTL cannot exceed the table TTL. For more information, see Configure the TTL of a search index.

Index schema

Name

Type

Description

FieldSchemas (required)

[]*tablestore.FieldSchema

The index field list.

IndexSetting (optional)

*tablestore.IndexSetting

The index settings.

IndexSort (optional)

*search.Sort

The index presorting configuration. If this parameter is omitted and the index contains no Nested field, the index is sorted by primary key. Presorting is not supported for an index that contains a Nested field.

Index fields

Name

Type

Description

FieldName (required)

*string

The index field name, which can correspond to a primary key or attribute column.

FieldType (required)

tablestore.FieldType

The data type of the index field.

Index (optional)

*bool

Specifies whether to create an inverted or spatial index. For fields other than Nested and JSON fields, an omitted value is treated as true.

IndexOptions (optional)

*tablestore.IndexOptions

The index-content granularity for a Text field. This parameter is typically omitted.

Analyzer (optional)

*tablestore.Analyzer

The analyzer for a Text field. If omitted, single-word tokenization is used.

AnalyzerParameter (optional)

interface{}

Analyzer parameters. Configure this parameter based on the Analyzer value.

EnableSortAndAgg (optional)

*bool

Specifies whether to enable sorting and aggregation. Text and Nested fields do not support this feature, but child fields of a Nested field do.

EnableHighlighting (optional)

*bool

Specifies whether to enable summary and highlighting. Only Text fields support this feature. Default value: false.

Store (optional)

*bool

Specifies whether to store the original field value in the search index. If enabled, ReturnAllFromIndex can return the value.

IsArray (optional)

*bool

Specifies whether the field is an array. Array values must be written in JSON array format. Do not configure this parameter for Nested fields.

FieldSchemas (optional)

[]*tablestore.FieldSchema

The child fields of a Nested or JSON field. This parameter is required for Nested and JSON fields.

IsVirtualField (optional)

*bool

Specifies whether the field is a virtual column. Default value: false.

SourceFieldNames (optional)

[]string

The source fields mapped to the virtual column. This parameter is required for a virtual column. Only one source field is supported.

DateFormats (optional)

[]string

The date formats supported by a Date field. This parameter is required for a Date field.

VectorOptions (optional)

*tablestore.VectorOptions

The vector configuration of a Vector field. This parameter is required for a Vector field.

JsonType (optional)

*tablestore.JsonType

The index type of a JSON field. Valid values: JsonType_OBJECT and JsonType_NESTED. This parameter is required for a JSON field.

TextSimilarity (optional)

*tablestore.TextSimilarity

The similarity algorithm for a Text field. Valid values: TextSimilarity_BM25 and TextSimilarity_SHORT_TEXT.

Vector configuration

Name

Type

Description

VectorDataType (required)

*tablestore.VectorDataType

The vector data type. Only VectorDataType_FLOAT_32 is supported.

VectorMetricType (required)

*tablestore.VectorMetricType

The distance metric. Euclidean distance, cosine similarity, and dot product are supported.

Dimension (required)

*int32

The vector dimension. Maximum value: 4096.

Index settings

Name

Type

Description

RoutingFields (optional)

[]string

Custom routing fields. You can specify one or more primary key columns. Rows with the same routing values are written to the same index partition.

Presorting configuration

Name

Type

Description

Sorters (required)

[]search.Sorter

The presorters. PrimaryKeySort and FieldSort are supported. A field used by FieldSort must enable sorting and aggregation. For more information, see Sort and paginate results.

Examples

Configure index presorting

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

Configure the index TTL

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

Configure an analyzer

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

Create a vector field

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

Create a virtual column

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

Enable summary and highlighting

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