All Products
Search
Document Center

Tablestore:Create a search index

Last Updated:Sep 18, 2024

You can call the CreateSearchIndex operation to create one or more search indexes for a data table. When you create a search index, you can add the fields that you want to query to the search index and configure advanced settings for the search index. For example, you can configure the routing key and presorting settings.

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.

  • A data table is created. The TimeToLive parameter is set to -1 and the MaxVersions parameter is set to 1 for the data table.

Usage notes

The data types of the fields in a search index must match the data types of the fields in the data table for which the search index is created. For more information, see Data type mappings.

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 the IndexSchema parameter. 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. Each field schema contains the following parameters:

  • FieldName: required. This parameter specifies the name of the field in the search index. The value is used as a column name. The value of this parameter is of the STRING type.

    A column in a search index can be a primary key column or an attribute column of the data table.

  • FieldType: required. This parameter specifies the type of the field. Specify the type in the FieldType.XXX format. For more information, see Data type mappings.

  • Array: optional. This parameter specifies whether the value is an array. The value of this parameter is of the BOOLEAN type.

    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 the FieldType parameter to Nested, this parameter is not required.

  • Index: optional. This parameter specifies whether to enable indexing for the column. The value of this parameter is of the BOOLEAN type.

    Default value: true. A value of true specifies that Tablestore indexes the column by using an inverted indexing or spatio-temporal indexing schema. A value of false specifies that indexing is disabled for the column.

  • Analyzer: optional. This parameter specifies the type of analyzer that you want to use. If you set the FieldType parameter to Text, you can specify this parameter. If you do not specify this parameter, the default analyzer type single-word tokenization is used. For more information, see Tokenization.

  • EnableSortAndAgg: optional. This parameter specifies whether to enable sorting and aggregation. The value of this parameter is of the BOOLEAN type.

    Sorting can be enabled only for fields for which the EnableSortAndAgg parameter is set to true. For more information, see Sorting and paging.

    Important

    Fields of the Nested type do not support sorting and aggregation. The 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. The value of this parameter is of the BOOLEAN type.

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

  • IsVirtualField: optional. This parameter specifies whether the field is a virtual column. The value of this parameter is of the BOOLEAN type. Default value: false. You must set this parameter to true for virtual columns. For more information about virtual columns, see Virtual columns.

  • SourceFieldNames: optional. This parameter specifies the name of the source field to which the virtual column is mapped in the data table. The value of this parameter is of the STRING type.

    Important

    This parameter is required if the IsVirtualField parameter is set to true.

  • DateFormats: optional. The format of dates. The value of this parameter is of the STRING type. For more information, see Date data type.

    Important

    This parameter is required if you set the FieldType parameter to DATE.

IndexSetting

The settings of the search index, including RoutingFields.

RoutingFields: optional. The 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 records whose routing field values are the same are distributed to the same partition.

IndexSort

The presorting settings for the search index, including Sorters. By default, if you do not specify this parameter, the field values are sorted based on the primary key.

Note

You can skip the presorting settings for search indexes that contain fields of the Nested type.

Sorters: required. The presorting method for the search index. PrimaryKeySort and FieldSort are supported. For more information, see Sorting and paging.

  • PrimaryKeySort: sorts data by primary key. You can specify the following parameter for the PrimaryKeySort parameter:

    Order: the sort order. Data can be sorted in ascending or descending order. Default value: DataModel.Search.Sort.SortOrder.ASC.

  • FieldSort: sorts data by field value. You can specify the following parameters for the FieldSort parameter:

    Only fields for which an index is created and the EnableSortAndAgg parameter 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

Optional. The time-to-live (TTL) of the search index. Default value: -1. The TTL of the search index is the retention period of data in the search index.

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

The value of this parameter must be greater than or equal to 86400. A value of 86400 specifies one day. You can also set this parameter to -1, which specifies that data never expires.

Examples

Create a search index by using the default configurations

The following sample code shows how to create a search index by using the default configurations. In this example, the search index consists of the following columns: the Keyword_type_col column of the Keyword type, the Long_type_col column of the Long type, and the Text_type_col column of the Text type. The sorting and aggregation are enabled.

/// <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 the Keyword_type_col column to Keyword, in the Long_type_col column to Long, and in the Text_type_col column 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);
}

Create a search index with the IndexSort parameter specified

The following sample code shows how to create a search index with the IndexSort parameter specified. In this example, the search index consists of the following columns: the Keyword_type_col column of the Keyword type, the Long_type_col column of the Long type, and the Text_type_col column of the Text type. The data in the search index is presorted based on the Long_type_col column.

/// <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 the Keyword_type_col column to Keyword, in the Long_type_col column to Long, and in the Text_type_col column to Text. 
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndexWithIndexSort(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,
        // Presort data based on 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);
}

Create a search index that contains a column of the Date type and a virtual column.

The following sample code shows how to create a search index that contains a column of the Date type and a virtual column. In this example, the search index consists of the following columns: the pk0 column of the Keyword type, the pk1 column of the Long type, the date_col column of the Date type, the geo_col column of the Geo-point type, and the col0_v1 column of the Text type. The source column of the col0_v1 virtual column is the col0 column. The returned results are sorted in ascending order based on the pk1 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);
}

FAQ

References

  • After you create a search index, you can use the query methods provided by the search index to query data from multiple dimensions based on your business requirements. The following query methods are supported: term query, terms query, match all query, match query, phrase query, prefix query, range query, wildcard query, geo query, Boolean query, nested query, and exists query.

    If you call the Search operation to query data, you can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.

  • If you call the Search operation to query data, you can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).

  • If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.

  • If you want to quickly obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Perform a parallel scan.

  • You can dynamically modify the schema of a search index to add, update, or remove index columns in the search index. For more information, see Dynamically modify the schema of a search index.

  • You can call the ListSearchIndex operation to query all search indexes that are created for a data table. For more information, see List search indexes.

  • You can call the DescribeSearchIndex operation to query the description of a search index. For example, you can query the field information and search index configurations. For more information, see Query the description of a search index.

  • You can delete a search index that you no longer need. For more information, see Delete a search index.