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

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialization.
  • A data table is created. The TimeToLive parameter is set to -1 and the MaxVersions parameter is set to 1 for the data table.

Parameters

When you create a search index, you must configure the TableName, IndexName, and IndexSchema parameters. You must also configure the FieldSchemas, IndexSetting, and IndexSort parameters 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: This parameter is required and 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: This parameter is required and specifies the type of the field. Use FieldType.XXX to set the type. For more information, see Data type mappings.
  • Array: This parameter is optional and 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: This parameter is optional and 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: This parameter is optional and 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: This parameter is optional and 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: This parameter is optional and 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.

  • IsVirtualField: This parameter is optional and specifies whether the field is a virtual column. Type: Boolean. Default value: false. You must set this parameter to true only when you use virtual columns. For more information about virtual columns, see Virtual columns.
  • SourceFieldNames: This parameter is optional and specifies the name of the source field to which the virtual column is mapped in the data table. Type: String.
    Important This parameter is required when IsVirtualField is set to true.
  • DateFormats: This parameter is optional and specifies the format of dates. Type: String. For more information, see Types of date data.
    Important This parameter is required when the field type is Date.
IndexSetting The settings of the search index, including RoutingFields.

RoutingFields: This parameter is optional and 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 for 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 the search indexes that contain fields of the Nested type.
Sorters: This parameter is required and 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: DataModel.Search.Sort.SortOrder.ASC.

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

    Only fields for which an index is created 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: DataModel.Search.Sort.SortOrder.ASC.
    • Mode: the sorting method that is used when the field contains multiple values.
TimeToLive This parameter is optional and specifies the retention period of data in the search index. Unit: seconds. Default value: -1.

If the retention period exceeds the TTL value, Tablestore automatically deletes expired data.

The minimum timeToLive value is 86400, which is equal to one day. A value of -1 specifies that data never expires.

Examples

  • Example 1

    The following sample code shows 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)
    {
        // Specify the names of the table and the search index. 
        CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
        List<FieldSchema> FieldSchemas = new List<FieldSchema>() {
            new FieldSchema(Keyword_type_col,FieldType.KEYWORD){ // Specify the name and type of the field. 
                index =true, // Enable the indexing feature. 
                EnableSortAndAgg = true // Enable sorting and aggregation. 
            },
            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 sample code shows how to create a search index with IndexSort specified:
    /// <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)
    {
        // Specify 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){ // Specify the name and type of the field. 
                index =true, // Enable the indexing feature. 
                EnableSortAndAgg = true // Enable sorting and aggregation. 
            },
            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 sorting and aggregation for the column. 
            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);
    }
  • Example 3

    The following sample code shows how to create a search index that contains a column of the Date type and a virtual column:

    /// <summary>
    /// Create a search index that contains a column of the Date type and a virtual column. 
    /// </summary>
    /// <param name="otsClient"></param>
    public static void CreateSearchIndex(OTSClient otsClient)
    {
        List<FieldSchema> fieldSchemas = new List<FieldSchema> {
            new FieldSchema("pk0", FieldType.KEYWORD)
            {
                index = true,
                EnableSortAndAgg = true
            },
    
            new FieldSchema("pk1", FieldType.LONG)
            {
                index = true,
                EnableSortAndAgg = true
            },
    
            new FieldSchema("date_col", FieldType.DATE)
            {
                index = true,
                DateFormats = new List<string>(){
                    "yyyy-MM-dd'T'HH:mm:ss.SSSSSS",
                    "yyyy-MM-dd'T'HH:mm:ss.SSS"
                }
            },
    
            new FieldSchema("geo_col", FieldType.GEO_POINT)
            {
                index = true,
                EnableSortAndAgg = true
            },
    
            new FieldSchema("col0_v1", FieldType.TEXT)
            {
                index = true,
                Analyzer = Analyzer.Split,
                AnalyzerParameter = new SingleWordAnalyzerParameter(true, true),
                IsVirtualField = true,
                SourceFieldNames = new List<string> { "col0" }
            },
        };
    
        CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName);
        request.IndexSchame = new IndexSchema()
        {
            FieldSchemas = fieldSchemas,
            IndexSort = new Sort(new List<ISorter> { new FieldSort("pk1", SortOrder.ASC) })
        };
        request.TimeToLive = -1;
    
        otsClient.CreateSearchIndex(request);
    }