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

Prerequisites

  • OTSClient is initialized. For more information, see Initialization.
  • A data table whose time_to_live is set to -1 and max_versions is set to 1 is created.

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 returned when a parameter error occurs or the Tablestore server returns a verification error. 
 * @throws OTSServerException The exception that is returned 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 table_name, index_name, and schema. You must specify field_schemas, index_setting, and index_sort in schema. The following table describes the parameters.

Parameter Description
table_name The name of the 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 a column name. Type: String.

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

  • field_type: required. This parameter specifies the type of the field. Use FieldTypeConst::XXX to set the type. For more information, see Data type mappings.
  • is_array: optional. This parameter 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 field_type to Nested, skip this parameter.

  • index: optional. This parameter specifies whether to create an index for the column. Type: Boolean.

    By default, this parameter is set to true, and Tablestore creates an inverted index or spatial index for the column. If this parameter is set to false, Tablestore does not create indexes for the column.

  • analyzer: optional. This parameter specifies the type of analyzer to use. If field_type is set to Text, you can set this parameter. If you do not specify this parameter, single-word tokenization is used. For more information about tokenization, see Tokenization.
  • enable_sort_and_agg: optional. This parameter specifies whether to enable the sorting and aggregation features. Type: Boolean.
    Before you enable the sorting feature, you need to set enable_sort_and_agg to true for a field. 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: optional. This parameter specifies whether to store the value of the field in the search index. Type: Boolean.
index_setting The settings of the search index, including routing_fields.

routing_fields: optional. This parameter specifies custom routing fields. You can specify some primary key columns as routing fields. Tablestore distributes data that is written to a search index to different partitions based on the specified routing fields. The data with the same routing field values is distributed to the same partition.

index_sort The presorting settings of the search index, including sorters. If index_sort is left empty, data is sorted by primary key.
Note You can skip the presorting settings for search indexes that contain the Nested field type.
sorters: required. This parameter 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: SortOrderConst::SORT_ORDER_ASC.

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

    You can presort field values only when a search index is created and the sorting and aggregation features are enabled for fields in the search index.

    • field_name: the name of the field to sort.
    • order: the sort order. Data can be sorted in ascending or descending order. Default value: SortOrderConst::SORT_ORDER_ASC.
    • mode: the sorting method used when the field has multiple values.

Examples

$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 the Nested field 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();