All Products
Search
Document Center

Tablestore:Create a search index

Last Updated:Nov 29, 2025

You can use the CreateSearchIndex method to create a search index for a data table. A data table can have multiple search indexes. When you create a search index, you must add the fields that you want to query to the index. You can also configure advanced options, such as custom routes and presorting.

Prerequisites

  • The Tablestore client is initialized. For more information, see Initialize a Tablestore client.

  • A data table is created. The `max versions` for the table must be 1, and the `time to live` must be -1. For more information, see Create a data table.

Usage notes

When you create a search index, the data types of the fields in the search index must match those of the corresponding fields in the data table.

API

/**
 * Create a search index.
 * @api
 *
 * @param [] $request
 *            The request parameters, such as the table name and index configuration.
 * @return [] The response.
 * @throws OTSClientException Thrown if a parameter check fails or the server returns a verification error.
 * @throws OTSServerException Thrown if 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 (`table_name`), search index name (`index_name`), and index schema (`schema`). The schema includes `field_schemas` (settings for all index fields), `index_setting` (index settings), and `index_sort` (index presorting settings). 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

A list of field schemas. Each `field_schema` contains the following parameters:

  • field_name (Required): The name of the field to index. This is the column name. The data type is String.

    A field in a search index can be a primary key column or an attribute column.

  • field_type: required. The field's data type, specified in the `FieldTypeConst::XXX` format.

  • is_array (Optional): Specifies whether the column is an array. The data type is Boolean.

    If set to true, the column is an array. Data written to the column must be in the JSON array format, such as `["a","b","c"]`.

    The Nested type is an array. If `field_type` is Nested, you do not need to set this parameter.

  • index (Optional): Specifies whether to create an index for the column. The data type is Boolean.

    The default value is true. This means an inverted index or a spatial index is built for the column. If set to false, no index is built for the column.

  • analyzer (Optional): The Tokenization type. If the field type is Text, you can set this parameter. Otherwise, the default tokenizer type is single-word tokenization.

  • enable_sort_and_agg (Optional): Specifies whether to enable sorting and statistical aggregation. The data type is Boolean.

    You can perform sorting on fields only if enable_sort_and_agg is set to true.

    Important

    Fields of the Nested type do not support sorting and statistical aggregation. However, sub-columns within a Nested type field support this feature.

index_setting

Index settings, which include the `routing_fields` setting.

routing_fields (Optional): Custom routing fields. You can select some primary key columns as routing fields. In most cases, you only need to set one. If you set multiple routing keys, the system concatenates their values into a single value.

When you write data to the index, the system calculates the data distribution location based on the values of the routing fields. Records with the same routing field values are indexed into the same data partition.

index_sort

Index presorting settings, which include the `sorters` setting. If this is not set, data is sorted by primary key by default.

Note

Indexes that contain Nested type fields do not support `index_sort` and are not presorted.

sorters: required. This parameter specifies 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. It includes the following setting:

    order: The sort order. Data can be sorted in ascending or descending order. The default is ascending (`SortOrderConst::SORT_ORDER_ASC`).

  • FieldSort: Sorts data by field value. It includes the following settings:

    Only fields that are indexed and have sorting and statistical aggregation enabled can be presorted.

    • field_name: The name of the field to sort by.

    • order: The sort order. Data can be sorted in ascending or descending order. The default is ascending (`SortOrderConst::SORT_ORDER_ASC`).

    • mode: The sorting method to use when a field has 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: a keyword column of the Keyword type, a text column of the Text type, a geo column of the Geo-point type, a long column of the Long type, a double column of the Double type, a boolean column of the Boolean type, an array column of the Keyword type, and a 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 is configured to never expire.

$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,
                'is_array' => false
            ),
            array(
                'field_name' => 'text',
                'field_type' => FieldTypeConst::TEXT,
                'analyzer' => 'single_word',
                'index' => true,
                'enable_sort_and_agg' => false,
                'is_array' => false
            ),
            array(
                'field_name' => 'geo',
                'field_type' => FieldTypeConst::GEO_POINT,
                'index' => true,
                'enable_sort_and_agg' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'long',
                'field_type' => FieldTypeConst::LONG,
                'index' => true,
                'enable_sort_and_agg' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'double',
                'field_type' => FieldTypeConst::DOUBLE,
                'index' => true,
                'enable_sort_and_agg' => true,
                'is_array' => false
            ),
            array(
                'field_name' => 'boolean',
                'field_type' => FieldTypeConst::BOOLEAN,
                'index' => true,
                'enable_sort_and_agg' => false,
                'is_array' => false
            ),
            array(
                'field_name' => 'array',
                'field_type' => FieldTypeConst::KEYWORD,
                'index' => true,
                'enable_sort_and_agg' => false,
                'is_array' => true
            ),
            array(
                'field_name' => 'nested',
                'field_type' => FieldTypeConst::NESTED,
                'index' => false,
                'enable_sort_and_agg' => false,
                'field_schemas' => array(
                    array(
                        'field_name' => 'nested_keyword',
                        'field_type' => FieldTypeConst::KEYWORD,
                        'index' => false,
                        'enable_sort_and_agg' => false,
                        'is_array' => false
                    )
                )
            ),
        ),
        'index_setting' => array(
            'routing_fields' => array("pk1")
        ),
//        "index_sort" => array(// Indexes that contain Nested type fields do not support index_sort and are not presorted.
//            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

Related documents