All Products
Search
Document Center

Tablestore:Create a search index

Last Updated:Apr 14, 2026

You can use the CreateSearchIndex method to create one or more search indexes for a data table. When you create a search index, you must add the fields that you want to query to the index. You can also configure advanced options, such as routing fields and pre-sorting.

Prerequisites

  • A Tablestore client is initialized. For more information, see Initialize a Tablestore client.

  • A data table is created and meets the following conditions. For more information, see Create a data table.

    • The maximum number of versions is set to 1.

    • The time to live (TTL) is set to -1, or the UpdateRow operation is disabled for the data table.

Usage notes

  • The data types of the fields in the search index must match the data types of the fields in the data table.

  • To set the TTL of a search index to a value other than -1, disable the UpdateRow operation for the data table. The TTL of the search index must be less than or equal to the TTL of the data table. For more information, see Lifecycle management.

Parameters

To create a search index, specify the table name (tableName), index name (indexName), and index schema (schema). The schema includes field schemas (fieldSchemas), index settings (indexSetting), and index pre-sorting settings (indexSort).

Parameter

Description

tableName

The name of the data table.

indexName

The name of the search index.

fieldSchemas

The list of field schemas. Each field schema contains the following parameters:

  • fieldName (required): The name of the field to index. This is the column name. Type: String.

    A field in a search index can be a primary key column or an attribute column.

  • fieldType (required): The data type of the field. Set this parameter to TableStore.FieldType.XXX.

  • index (optional): Whether to enable indexing. Type: Boolean.

    Default value: true, which creates an inverted index or spatial index for the column. Set to false to skip indexing for the column.

  • analyzer (optional): The tokenizer type. Set this parameter when the field type is Text. If not set, single-word tokenization is used by default.

  • analyzerParameter (optional): The tokenizer parameter settings. Configure the parameters based on the tokenizer type. Required if the analyzer parameter is set for a field.

  • enableSortAndAgg (optional): Whether to enable sorting and statistical aggregation. Type: Boolean.

    Only fields with enableSortAndAgg set to true support sorting.

    Important

    Nested fields do not support sorting and statistical aggregation. However, the sub-columns within a Nested field support sorting and statistical aggregation.

  • isAnArray (optional): Whether the field is an array. Type: Boolean.

    If set to true, the column is an array. Write data in JSON array format, such as ["a","b","c"].

    Nested types are inherently arrays, so this parameter is not required when fieldType is Nested.

  • fieldSchemas (optional): When the field type is Nested, use this parameter to set the index types of sub-columns in the nested document. The type is a list of field schemas.

  • isVirtualField (optional): Whether the field is a virtual column. Type: Boolean. Default value: false. Set to true to use a virtual column.

  • sourceFieldName (optional): The name of the source field in the data table. Type: String. Required if isVirtualField is set to true.

  • dateFormats (optional): The date format. Type: String. Required if the field type is Date. For more information, see Date and time data types.

  • enableHighlighting (optional): Whether to enable summary and highlighting. Type: Boolean. Default value: false. Set to true to enable this feature. Only Text fields support highlighting.

    Important

    This feature is supported in Tablestore SDK for Node.js 5.5.0 and later.

  • vectorOptions (optional): The vector field properties. Required for fields of the Vector type. It includes the following parameters:

    • dataType: The data type of the vector. Only float32 is supported. For other data type requirements, submit a ticket.

    • dimension: The number of vector dimensions. Maximum: 4,096.

    • metricType: The distance measurement algorithm for vectors. Supported algorithms: Euclidean distance (euclidean), cosine similarity (cosine), and dot product (dot_product).

      • Euclidean distance (euclidean): Measures the straight-line distance between two vectors in a multidimensional space. For performance reasons, Tablestore skips the final square root calculation. A larger score indicates higher similarity.

      • Cosine similarity (cosine): Measures the cosine of the angle between two vectors. A higher score indicates higher similarity. Commonly used for text data.

      • Dot product (dot_product): Multiplies corresponding coordinates of two same-dimension vectors and sums the results. A higher score indicates higher similarity.

      For guidance on selecting a distance metric algorithm, see Distance metric algorithms.

indexSetting

The index settings, which include the routingFields setting.

routingFields (optional): The custom routing fields. You can select some primary key columns as routing fields. In most cases, you only need to set one. If you set multiple routing keys, the system concatenates the values of the routing keys into a single value.

During index data writes, the system distributes index data based on routing field values. Records with the same routing field values are stored in the same data partition.

indexSort

The index pre-sorting settings, which include the sorters setting. If not set, data is sorted by primary key by default.

Note

Indexes that contain Nested fields do not support indexSort. No pre-sorting is performed.

sorters (required): The pre-sorting method for the index. Supports sorting by primary key or by field value. For more information, see Sorting and paging.

  • PrimaryKeySort sorts by primary key. It includes the following setting:

    order: The sort order. Supports ascending or descending order. Default: ascending (TableStore.SortOrder.SORT_ORDER_ASC).

  • FieldSort sorts by field value. It includes the following settings:

    Pre-sorting applies only to fields that have an index and sorting and statistical aggregation enabled.

    • fieldName: The name of the field to sort by.

    • order: The sort order. Supports ascending or descending order. Default: ascending (TableStore.SortOrder.SORT_ORDER_ASC).

    • mode: The sorting method to use when a field has multiple values.

timeToLive

Optional. The time-to-live (TTL) of data, which is the data retention period. Unit: seconds.

Default value: -1, which means data never expires. The minimum TTL value is 86400 seconds (one day).

When the data retention period exceeds the TTL, expired data is automatically cleaned up.

Examples

Create a search index and set a tokenizer

The following example creates a search index with these columns: pic_id (Keyword), count (Long), time_stamp (Long), pic_description (Text), col_vector (Vector), pos (Geo-point), pic_tag (Nested), date (Date), analyzer_single_word (Text), analyzer_split (Text), and analyzer_fuzzy (Text). The pic_tag column has two sub-fields: sub_tag_name (Keyword) and tag_name (Keyword). The analyzer_single_word column uses single-word tokenization, analyzer_split uses delimiter tokenization, and analyzer_fuzzy uses fuzzy tokenization.

client.createSearchIndex({
    tableName: "<TABLE_NAME>", // Set the table name.
    indexName: "<INDEX_NAME>", // Set the search index name.
    schema: {
        fieldSchemas: [
            {
                fieldName: "pic_id",
                fieldType: TableStore.FieldType.KEYWORD, // Set the field name and field type.
                index: true, // Enable indexing.
                enableSortAndAgg: true, // Enable sorting and aggregation.
                isAnArray: false
            },
            {
                fieldName: "count",
                fieldType: TableStore.FieldType.LONG,
                index: true,
                enableSortAndAgg: true,
                isAnArray: false
            },
            {
                fieldName: "time_stamp",
                fieldType: TableStore.FieldType.LONG,
                index: true,
                enableSortAndAgg: false,
                isAnArray: false,
            },
            {
                fieldName: "pic_description",
                fieldType: TableStore.FieldType.TEXT,
                index: true,
                enableSortAndAgg: false,
                isAnArray: false,
            },
            {
                fieldName: "col_vector",
                fieldType: TableStore.FieldType.VECTOR,
                index: true,
                isAnArray: false,
                vectorOptions: {
                    dataType: TableStore.VectorDataType.VD_FLOAT_32,
                    dimension: 4,
                    metricType: TableStore.VectorMetricType.VM_COSINE,
                }
            },
            {
                fieldName: "pos",
                fieldType: TableStore.FieldType.GEO_POINT,
                index: true,
                enableSortAndAgg: true,
                isAnArray: false,
            },
            {
                fieldName: "pic_tag",
                fieldType: TableStore.FieldType.NESTED,
                index: false,
                enableSortAndAgg: false,
                fieldSchemas: [
                    {
                        fieldName: "sub_tag_name",
                        fieldType: TableStore.FieldType.KEYWORD,
                        index: true,
                        enableSortAndAgg: true,
                    },
                    {
                        fieldName: "tag_name",
                        fieldType: TableStore.FieldType.KEYWORD,
                        index: true,
                        enableSortAndAgg: true,
                    }
                ]
            },
            {
                fieldName: "date",
                fieldType: TableStore.FieldType.DATE,
                index: true,
                enableSortAndAgg: true,
                isAnArray: false,
                dateFormats: ["yyyy-MM-dd'T'HH:mm:ss.SSSSSS"],
            },
            {
                fieldName: "analyzer_single_word",
                fieldType: TableStore.FieldType.TEXT,
                analyzer: "single_word",
                index: true,
                enableSortAndAgg: false,
                isAnArray: false,
                analyzerParameter: {
                    caseSensitive: true,
                    delimitWord: false,
                }
            },
            {
                fieldName: "analyzer_split",
                fieldType: TableStore.FieldType.TEXT,
                analyzer: "split",
                index: true,
                enableSortAndAgg: false,
                isAnArray: false,
                analyzerParameter: {
                    delimiter: ",",
                }
            },
            {
                fieldName: "analyzer_fuzzy",
                fieldType: TableStore.FieldType.TEXT,
                analyzer: "fuzzy",
                index: true,
                enableSortAndAgg: false,
                isAnArray: false,
                analyzerParameter: {
                    minChars: 1,
                    maxChars: 5,
                }
            },
        ],
        indexSetting: { // The configuration options of the index.
            "routingFields": ["count", "pic_id"], // Only primary key columns can be set as routing fields.
            "routingPartitionSize": null
        },
        //indexSort: {// indexSort is not supported for indexes that contain Nested fields. No pre-sorting is performed.
            //sorters: [
                // { // If you do not set indexSort, the data is sorted by primary key in ascending order by default.
                //     primaryKeySort: {
                //         order: TableStore.SortOrder.SORT_ORDER_ASC
                //     }
                // },
                //{
                //   fieldSort: {
                //        fieldName: "Col_Keyword",
                //        order: TableStore.SortOrder.SORT_ORDER_DESC // Set the sorting order for indexSort.
                //    }
                //}
            //]
        //},
        timeToLive: 1000000, // Unit: seconds.
    }
}, function (err, data) {
    if (err) {
        console.log('error:', err);
        return;
    }
    console.log('success:',data);
});

Create a search index and enable highlighting

The following example creates a search index with highlighting enabled. The index has three fields: k (Keyword), t (Text), and n (Nested). The n field has three sub-fields: nk (Keyword), nl (Long), and nt (Text). Highlighting is enabled for the t field and the nt sub-field.

client.createSearchIndex({
    tableName: "<TABLE_NAME>", // Set the table name.
    indexName: "<SEARCH_INDEX_NAME>", // Set the search index name.
    schema: {
        fieldSchemas: [
            {
                fieldName: "k",
                fieldType: TableStore.FieldType.KEYWORD, // Set the field name and field type.
                index: true, // Enable indexing.
                enableSortAndAgg: true, // Enable sorting and aggregation.
                isAnArray: false
            },
            {
                fieldName: "t",
                fieldType: TableStore.FieldType.TEXT,
                index: true,
                enableSortAndAgg: false,
                enableHighlighting: true, // Enable highlighting for the field.
                isAnArray: false,
            },
            {
                fieldName: "n",
                fieldType: TableStore.FieldType.NESTED,
                index: false,
                enableSortAndAgg: false,
                fieldSchemas: [
                    {
                        fieldName: "nk",
                        fieldType: TableStore.FieldType.KEYWORD,
                        index: true,
                        enableSortAndAgg: true,
                    },
                    {
                        fieldName: "nl",
                        fieldType: TableStore.FieldType.LONG,
                        index: true,
                        enableSortAndAgg: true,
                    },
                    {
                        fieldName: "nt",
                        fieldType: TableStore.FieldType.TEXT,
                        index: true,
                        enableSortAndAgg: false,
                        enableHighlighting: true, // Enable highlighting for the field.
                    },
                ]
            },
        ],
        indexSetting: { // The configuration options of the index.
            "routingFields": ["id"], // Only primary key columns can be set as routing fields.
            "routingPartitionSize": null
        },
        //indexSort: {// indexSort is not supported for indexes that contain Nested fields. No pre-sorting is performed.
            //sorters: [
                // { // If you do not set indexSort, the data is sorted by primary key in ascending order by default.
                //     primaryKeySort: {
                //         order: TableStore.SortOrder.SORT_ORDER_ASC
                //     }
                // },
                //{
                //   fieldSort: {
                //        fieldName: "Col_Keyword",
                //        order: TableStore.SortOrder.SORT_ORDER_DESC // Set the sorting order for indexSort.
                //    }
                //}
            //]
        //},
        timeToLive: 1000000, // Unit: seconds.
    }
}, function (err, data) {
    if (err) {
        console.log('error:', err);
        return;
    }
    console.log('success:',data);
});

FAQ

References