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

Prerequisites

  • OTSClient is initialized. For more information, see Initialize a Tablestore client.
  • A data table whose TimeToLive is set to -1 and MaxVersions is set to 1 is created.

Parameters

When you create a search index, you must specify TableName, IndexName, and IndexSchema. You must specify FieldSchemas, IndexSetting, and IndexSort in IndexSchema. The following table describes the parameters.
Parameter Description
TableName The name of the 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 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 types of column values.
  • Array: 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"].

    Nested values are an array. If you set FieldType to Nested, skip this parameter.

  • Index: optional. This parameter specifies whether to create an index for the column. Type: Boolean.

    By default, this parameter is set to true, and Tablestore creates an inverted index or spatial index for the column. If this parameter is set to false, Tablestore does not create indexes for the column.

  • Analyzer: optional. This parameter specifies the type of analyzer to use. If FieldType is set to Text, you can set this parameter. If you do not specify this parameter, single-word tokenization is used. For more information about tokenization, see Tokenization.
  • EnableSortAndAgg: optional. This parameter specifies whether to enable the sorting and aggregation features. Type: Boolean.

    Before you enable the sorting feature, you need to set EnableSortAndAgg to true for a field. For more information about sorting, see Sorting and pagination.

  • Store: optional. This parameter specifies whether to store the value of the field in the search index. Type: Boolean.

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

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 to different partitions based on the specified routing fields. The data with the same routing field values is distributed to the same partition.

IndexSort The presorting settings of the search index, including Sorters. If IndexSort is left empty, data is sorted by primary key.
Note You can skip the presorting settings for the search indexes that contain the Nested field 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 pagination.
  • 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. By default, data is sorted in ascending order.

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

    You can presort field values only when a search index is created and the sorting and aggregation features are enabled for fields in the search index.

    • FieldName: the name of the field for sorting.
    • Order: the sort order. Data can be sorted in ascending or descending order. By default, data is sorted in ascending order.
    • Mode: the sorting method used when the field has multiple values.

Examples

/**
 *Create a search index that contains the Col_Keyword and Col_Long columns. Set the type of data in Col_Keyword to Keyword. Set the type of data in Col_Long to Long. 
 */
func CreateSearchIndex(client *tablestore.TableStoreClient, tableName string, indexName string) {
    request := &tablestore.CreateSearchIndexRequest{}
    request.TableName = tableName // Set the name of the table. 
    request.IndexName = indexName // Set the name of the search index. 

    schemas := []*tablestore.FieldSchema{}
    field1 := &tablestore.FieldSchema{
        FieldName: proto.String("Col_Keyword"), // Set the field name by calling the proto.String method. This method is used to request a string pointer. 
        FieldType: tablestore.FieldType_KEYWORD, // Set the field type. 
        Index:     proto.Bool(true), // Enable the indexing feature. 
        EnableSortAndAgg: proto.Bool(true), // Enable the sorting and aggregation features. 
    }
    field2 := &tablestore.FieldSchema{
        FieldName: proto.String("Col_Long"),
        FieldType: tablestore.FieldType_LONG,
        Index:     proto.Bool(true),
        EnableSortAndAgg: proto.Bool(true),
    }
    schemas = append(schemas, field1, field2)

    request.IndexSchema = &tablestore.IndexSchema{
        FieldSchemas: schemas, // Set the fields included in the search index. 
    }
    resp, err := client.CreateSearchIndex(request) // Call a client to create the search index. 
    if err != nil {
        fmt.Println("error :", err)
        return
    }
    fmt.Println("CreateSearchIndex finished, requestId:", resp.ResponseInfo.RequestId)
}