All Products
Search
Document Center

Tablestore:Create a search index

Last Updated:Apr 14, 2026

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

Usage notes

The data types of fields in the search index must match the data types 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

Specify the table name (`table_name`), search index name (`index_name`), and index schema (`schema`). The schema includes `field_schemas` (field settings), `index_setting` (index settings), and `index_sort` (presorting settings).

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` includes the following parameters:

  • field_name (Required): The name of the field to index, which is the column name. Data type: String.

    The field can be a primary key column or an attribute column.

  • field_type (Required): The data type of the field, in the `FieldTypeConst::XXX` format.

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

    If set to true, the column stores array data. Write data in JSON array format, such as `["a","b","c"]`.

    Nested fields are arrays by default. If `field_type` is Nested, skip this parameter.

  • index (Optional): Specifies whether to enable indexing for the column. Data type: Boolean.

    Default: true, which builds an inverted index or a spatial index for the column. Set to false to skip indexing.

  • analyzer (Optional): The tokenization type. Applicable only to Text fields. Default: single-word tokenization.

  • enable_sort_and_agg (Optional): Specifies whether to enable sorting and aggregation. Data type: Boolean.

    Only fields with enable_sort_and_agg set to true support sorting.

    Important

    Nested type fields do not support sorting and aggregation. However, sub-columns within a Nested field support this feature.

index_setting

Index settings, including 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 index data is written, the system calculates the distribution location of the index data based on the values of the routing fields. Records with the same routing field values are indexed to the same data partition.

index_sort

Index presorting settings, including the `sorters` setting. Default: sorted by primary key.

Note

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

sorters (Required): The presorting method for the search index. Supports PrimaryKeySort and FieldSort. For more information, see Sorting and paging.

  • PrimaryKeySort: Sorts data by primary key. Includes the following setting:

    order: The sort order, ascending or descending. Default: ascending (`SortOrderConst::SORT_ORDER_ASC`).

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

    Only indexed fields with sorting and aggregation enabled can be presorted.

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

    • order: The sort order, ascending or descending. Default: ascending (`SortOrderConst::SORT_ORDER_ASC`).

    • mode: The sorting method to use when a field has multiple values.

Example

Create a search index with the following columns: keyword (Keyword), text (Text), geo (Geo-point), long (Long), double (Double), boolean (Boolean), array (Keyword), and nested (Nested). The nested column contains a nested_keyword subcolumn of the Keyword type. The index is presorted by the primary key and 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,
                '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

References