All Products
Search
Document Center

Tablestore:Create a search index

Last Updated:Aug 19, 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 for which the maxVersions parameter is set to 1 and the timeToLive parameter is set to -1 is created. For more information, see Create a 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.

API operation

/**
 * Create a search index. 
 * @api
 *
 * @param [] $request
 *            The request parameters, including the table name and index configuration. 
 * @return [] The response. 
 * @throws OTSClientException The exception that is thrown when a parameter error occurs or the Tablestore server returns a verification error. 
 * @throws OTSServerException The exception that is thrown when the Tablestore server returns an error. 
 * @example "src/examples/CreateSearchIndex.php"
 */
public function createSearchIndex(array $request)

Parameters

When you create a search index, you must specify the table_name, index_name, and schema parameters. In the schema parameter, specify the field_schemas, index_setting, and index_sort parameters. The following table describes the parameters.

Parameter

Description

table_name

The name of the data table.

index_name

The name of the search index.

field_schemas

The list of field schemas. Each field schema contains the following parameters:

  • field_name: 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.

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

  • is_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 field_type 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 with an inverted indexing schema or a spatio-temporal indexing schema. A value of false specifies that Tablestore does not enable indexing for the column.

  • analyzer: optional. This parameter specifies the type of analyzer that you want to use. If you set the field_type 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.

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

    Sorting can be performed only for fields for which the enable_sort_and_agg parameter is set to true. For more information, see Perform 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.

index_setting

The settings of the search index, including the routing_fields parameter.

routing_fields: optional. This parameter specifies custom routing fields. You can specify multiple 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 that has the same routing field values is distributed to the same partition.

index_sort

The presorting settings of the search index, including the sorters parameter. If you do not specify the index_sort parameter, field values are sorted by primary key.

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, see Perform 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: SortOrderConst::SORT_ORDER_ASC. This specifies that data is sorted in ascending order.

  • 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 enable_sort_and_agg parameter is set to true can be presorted.

    • field_name: 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: SortOrderConst::SORT_ORDER_ASC. This specifies that data is sorted in ascending order.

    • mode: the sorting method that you want to use when the field contains multiple values.

Example

The following sample code shows how to create a search index. In this example, the search index consists of the following columns: the keyword column of the Keyword type, the text column of the Text type, the geo column of the Geo-point type, the long column of the Long type, the double column of the Double type, the boolean column of the Boolean type, the array column of the Keyword type, and the nested column of the Nested type. The nested column contains a nested_keyword subcolumn of the Keyword type. The data in the search index is presorted based on the primary key of the data table and data in the search index never expires.

$request = array(
    'table_name' => 'php_sdk_test',
    'index_name' => 'php_sdk_test_search_index',
    'schema' => array(
        'field_schemas' => array(
            array(
                'field_name' => 'keyword',
                'field_type' => FieldTypeConst::KEYWORD,
                'index' => true,
                'enable_sort_and_agg' => true,
                'store' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'text',
                'field_type' => FieldTypeConst::TEXT,
                'analyzer' => 'single_word',
                'index' => true,
                'enable_sort_and_agg' => false,
                'store' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'geo',
                'field_type' => FieldTypeConst::GEO_POINT,
                'index' => true,
                'enable_sort_and_agg' => true,
                'store' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'long',
                'field_type' => FieldTypeConst::LONG,
                'index' => true,
                'enable_sort_and_agg' => true,
                'store' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'double',
                'field_type' => FieldTypeConst::DOUBLE,
                'index' => true,
                'enable_sort_and_agg' => true,
                'store' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'boolean',
                'field_type' => FieldTypeConst::BOOLEAN,
                'index' => true,
                'enable_sort_and_agg' => false,
                'store' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'array',
                'field_type' => FieldTypeConst::KEYWORD,
                'index' => true,
                'enable_sort_and_agg' => false,
                'store' => true,
                'is_array' => true
            ),
            array(
                'field_name' => 'nested',
                'field_type' => FieldTypeConst::NESTED,
                'index' => false,
                'enable_sort_and_agg' => false,
                'store' => false,
                'field_schemas' => array(
                    array(
                        'field_name' => 'nested_keyword',
                        'field_type' => FieldTypeConst::KEYWORD,
                        'index' => false,
                        'enable_sort_and_agg' => false,
                        'store' => false,
                        'is_array' => false
                    )
                )
            ),
        ),
        'index_setting' => array(
            'routing_fields' => array("pk1")
        ),
//        "index_sort" => array(// You can skip the presorting settings for search indexes that contain fields of the Nested type. 
//            array(
//                'field_sort' => array(
//                    'field_name' => 'keyword',
//                    'order' => SortOrderConst::SORT_ORDER_ASC,
//                    'mode' => SortModeConst::SORT_MODE_AVG,
//                )
//            ),
//            array(
//                'pk_sort' => array(
//                    'order' => SortOrderConst::SORT_ORDER_ASC
//                )
//            ),
//        )
    )
);
$response = $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 Perform 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 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 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 require. For more information, see Delete search indexes.