You can call the CreateSearchIndex operation to create one or more search indexes for a data table.

Prerequisites

  • A Tablestore client is initialized. For more information, see Initialization.
  • A data table is created for which the value of the timeToLive parameter is set to -1 and the value of the maxVersions parameter is set to 1.

Parameters

When you create a search index, you must specify tableName, indexName, and schema. You must also specify fieldSchemas, indexSetting, and indexSort in schema. The following table describes the parameters.

Parameter Description
tableName The name of the data table.
indexName The name of the search index.
fieldSchemas The list of field schemas. You can configure the following parameters for each field schema:
  • fieldName: required. This parameter specifies the name of the field in the search index. The value is used as a column name. Type: String.

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

  • fieldType: required. This parameter specifies the type of the field. Use TableStore.FieldType.XXX to set the type. For more information, see Data type mappings.
  • index: optional. This parameter specifies whether to enable indexing for the column. Type: Boolean.

    Default value: true. A value of true indicates that Tablestore indexes the column with an inverted indexing schema or a spatio-temporal indexing schema. A value of false indicates that Tablestore does not enable indexing for the column.

  • analyzer: optional. This parameter specifies the type of the analyzer that you want to use. If fieldType is set to Text, you can configure this parameter. Otherwise, the default analyzer type single-word tokenization is used. For more information about tokenization, see Tokenization.
  • enableSortAndAgg: optional. This parameter specifies whether to enable sorting and aggregation. Type: Boolean.
    Sorting can be enabled only for fields for which enableSortAndAgg is set to true. For more information about sorting, see Sorting and paging.
    Important Fields of the Nested type do not support sorting and aggregation, but subcolumns of fields of the Nested type support sorting and aggregation.
  • store: optional. This parameter specifies whether to store the value of the field in the search index. Type: Boolean.

    If you set store to true, you can read the value of the field from the search index without querying the data table. This improves query performance.

  • isAnArray: optional. This parameter specifies whether the value is an array. Type: Boolean.

    If you set this parameter to true, the column stores data as an array. Data written to the column must be a JSON array. Example: ["a","b","c"].

    The values of fields of the Nested type are arrays. If you set fieldType to Nested, skip this parameter.

  • fieldSchemas: optional. This parameter specifies the list of field schemas for subfields. If the column is a Nested column, you must specify this parameter to configure the index types of subcolumns in the Nested column.
  • isVirtualField: optional. This parameter specifies whether the field is a virtual column. Type: Boolean. Default value: false. This parameter is required only when you use virtual columns. For more information about virtual columns, see Virtual columns.
  • sourceFieldName: optional. This parameter specifies the name of the source field to which the virtual column is mapped in the data table. Type: String. This parameter is required when isVirtualField is set to true.
  • dateFormats: optional. This parameter specifies the format of dates. Type: String. This parameter is required when the field type is Date. For more information, see Types of date data.
indexSetting The settings of the search index, including routingFields.

routingFields: optional. This parameter specifies custom routing fields. You can specify some primary key columns as routing fields. Tablestore distributes data that is written to a search index across different partitions based on the specified routing fields. The data whose routing field values are the same is distributed to the same partition.

indexSort The presorting settings of the search index, including sorters. If no value is specified for the indexSort parameter, field values are sorted by primary key by default.
Note You can skip the presorting settings for search indexes that contain fields of the Nested type.
sorters: required. This parameter specifies the presorting method for the search index. PrimaryKeySort and FieldSort are supported. For more information about sorting, see Sorting and paging.
  • PrimaryKeySort: Data is sorted by primary key. You can configure the following parameter for PrimaryKeySort:

    order: the sort order. Data can be sorted in ascending or descending order. Default value: TableStore.SortOrder.SORT_ORDER_ASC.

  • FieldSort: Data is sorted by field value. You can configure the following parameters for FieldSort:

    Only fields for which indexing is enabled and enableSortAndAgg is set to true can be presorted.

    • fieldName: the name of the field that is used to sort data.
    • order: the sort order. Data can be sorted in ascending or descending order. Default value: TableStore.SortOrder.SORT_ORDER_ASC.
    • mode: the sorting method that is used when the field contains multiple values.

Examples

/**
 *Create a search index that contains columns Col_Keyword, Col_Long, Col_Text, and Col_Nested. 
 *Set the type of data in Col_Keyword to Keyword, in Col_Long to Long, in Col_Text to Text, and in Col_Nested to Nested. 
 */
client.createSearchIndex({
    tableName: TABLE_NAME, // Specify the name of the data table. 
    indexName: INDEX_NAME, // Specify the name of the search index. 
    schema: {
        fieldSchemas: [
            {
                fieldName: "Col_Keyword",
                fieldType: TableStore.FieldType.KEYWORD, // Specify the name and type of the field. 
                index: true, // Enable the indexing feature. 
                enableSortAndAgg: true, // Enable the sorting and aggregation features. 
                store: false,
                isAnArray: false
            },
            {
                fieldName: "Col_Long",
                fieldType: TableStore.FieldType.LONG,
                index: true,
                enableSortAndAgg: true,
                store: true,
                isAnArray: false
            },
            {
                fieldName: "Col_Text",
                fieldType: TableStore.FieldType.TEXT,
                index: true,
                enableSortAndAgg: false,
                store: true,
                isAnArray: false,
                analyzer: "single_word"
            },
            // {
            //     fieldName: "Col_Nested",
            //     fieldType: TableStore.FieldType.NESTED,
            //     index: false,
            //     enableSortAndAgg: false,
            //     store: false,
            //     fieldSchemas: [ // Specify the schemas of subfields in a Nested field. 
            //         {
            //             fieldName: "Sub_Col_KeyWord",
            //             fieldType: TableStore.FieldType.KEYWORD,
            //             index: true,
            //             enableSortAndAgg: true,
            //             store: false
            //         },
            //         {
            //             fieldName: "Sub_Col_Long",
            //             fieldType: TableStore.FieldType.LONG,
            //             index: true,
            //             enableSortAndAgg: true,
            //             store: false
            //         }
            //     ]
            // }
        ],
        indexSetting: { // Configure the search index. 
            "routingFields": ["Pk_Keyword"], // Only primary key columns can be set to routing fields. 
            "routingPartitionSize": null
        },
        indexSort: {// Search indexes that contain Nested fields do not support indexSort. 
            sorters: [
                // { // If indexSort is not specified, 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 // Specify the index sorting order. 
                    }
                }
            ]
        },
        timeToLive: 1000000, // Unit: seconds. 
    }
}, function (err, data) {
    if (err) {
        console.log('error:', err);
        return;
    }
    console.log('success:', data);
});