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 Initialization.
  • A data table whose TimeToLive is set to -1 and MaxVersions is set to 1 is created.

Parameter

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 used as the 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 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. Otherwise, the type of the analyzer is set to single-word tokenization. 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 data 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. Default value: DataModel.Search.Sort.SortOrder.ASC.

  • 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. Default value: DataModel.Search.Sort.SortOrder.ASC.
    • Mode: the sorting method used when the field has multiple values.

Examples

  • Example 1

    The following code provides an example on how to create a search index.

    /// <summary>
    /// Create a search index that contains the Keyword_type_col, Long_type_col, and Text_type_col attribute columns. Set the type of data in Keyword_type_col to Keyword, in Long_type_col to Long, and in Text_type_col to Text. 
    /// </summary>
    /// <param name="otsClient"></param>
    public static void CreateSearchIndex(OTSClient otsClient)
    {
        // Set the names of the table and search index. 
        CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
        List<FieldSchema> FieldSchemas = new List<FieldSchema>() {
            new FieldSchema(Keyword_type_col,FieldType.KEYWORD){ // Set the name and type of the field. 
                index =true, // Enable the indexing feature. 
                EnableSortAndAgg = true // Enable the sorting and aggregation features. 
            },
            new FieldSchema(Long_type_col,FieldType.LONG){ index=true,EnableSortAndAgg=true},
            new FieldSchema(Text_type_col,FieldType.TEXT){ index=true}
        };
        request.IndexSchame = new IndexSchema()
        {
            FieldSchemas = FieldSchemas
        };
        // Call a client to create the search index. 
        CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);
        Console.WriteLine("Searchindex is created: " + IndexName);
    }
  • Example 2
    The following code provides an example on how to specify IndexSort when you create a search index.
    /// <summary>
    /// Create a search index that contains the Keyword_type_col, Long_type_col, and Text_type_col attribute columns. Set the type of data in Keyword_type_col to Keyword, in Long_type_col to Long, and in Text_type_col to Text. 
    /// </summary>
    /// <param name="otsClient"></param>
    public static void CreateSearchIndexWithIndexSort(OTSClient otsClient)
    {
        // Set the names of the table and search index. 
        CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
        List<FieldSchema> FieldSchemas = new List<FieldSchema>() {
            new FieldSchema(Keyword_type_col,FieldType.KEYWORD){ // Set the name and type of the field. 
                index = true, // Enable the indexing feature. 
                EnableSortAndAgg = true // Enable the sorting and aggregation features. 
            },
            new FieldSchema(Long_type_col,FieldType.LONG){ index=true,EnableSortAndAgg=true},
            new FieldSchema(Text_type_col,FieldType.TEXT){ index=true}
        };
        request.IndexSchame = new IndexSchema()
        {
            FieldSchemas = FieldSchemas,
            // Presort data by the Long_type_col column. You must index the Long_type_col column and enable the sorting and aggregation features. 
            IndexSort = new DataModel.Search.Sort.Sort()
            {
                Sorters = new List<DataModel.Search.Sort.ISorter>
                {
                    new DataModel.Search.Sort.FieldSort(Long_type_col, DataModel.Search.Sort.SortOrder.ASC)
                }
            }
        };
    
        CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);
        Console.WriteLine("Searchindex is created: " + IndexName);
    }