使用CreateSearchIndex接口在数据表上创建一个多元索引。一个数据表可以创建多个多元索引。

前提条件

  • 已初始化Client。具体操作,请参见初始化
  • 已创建数据表,且数据表的数据生命周期(time_to_live)必须为-1,最大版本数(max_versions)必须为1。

接口

/**
 * 创建多元索引。
 * @api
 *
 * @param [] $request
 *            请求参数、数据表名称、索引配置等。
 * @return [] 请求返回。
 * @throws OTSClientException 当参数检查出错或服务端返回校验出错时抛出异常。
 * @throws OTSServerException 当OTS服务端返回错误时抛出异常。
 * @example "src/examples/CreateSearchIndex.php"
 */
public function createSearchIndex(array $request)

参数

创建多元索引时,需要指定数据表名称(table_name)、多元索引名称(index_name)和索引的结构信息(schema),其中schema包含field_schemas(Index的所有字段的设置)、index_setting(索引设置)和index_sort(索引预排序设置)。详细参数说明请参见下表。

参数 说明
table_name 数据表名称。
index_name 多元索引名称。
field_schemas field_schema的列表,每个field_schemas包含如下内容:
  • field_name(必选):建立索引的字段名,即列名,类型为String。

    多元索引中的字段可以是主键列或者属性列。

  • field_type(必选):字段类型,类型为FieldTypeConst::XXX。更多信息,请参见数据类型映射
  • is_array(可选):是否为数组,类型为Boolean。

    如果设置为true,则表示该列是一个数组,在写入时,必须按照JSON数组格式写入,例如["a","b","c"]。

    由于Nested类型是一个数组,当field_type为Nested类型时,无需设置此参数。

  • index(可选):是否开启索引,类型为Boolean。

    默认为true,表示对该列构建倒排索引或者空间索引;如果设置为false,则不会对该列构建索引。

  • analyzer(可选):分词器类型。当字段类型为Text时,可以设置此参数;如果不设置,则默认分词器类型为单字分词。关于分词的更多信息,请参见分词
  • enable_sort_and_agg(可选):是否开启排序与统计聚合功能,类型为Boolean。
    只有enable_sort_and_agg设置为true的字段才能进行排序。关于排序的更多信息,请参见 排序和翻页
    重要 Nested类型的字段不支持开启排序与统计聚合功能,但是Nested类型内部的子列支持开启排序与统计聚合功能。
  • store(可选):是否在多元索引中附加存储该字段的值,类型为Boolean。
index_setting 索引设置,包含routing_fields设置。

routing_fields(可选):自定义路由字段。可以选择部分主键列作为路由字段,在进行索引数据写入时,会根据路由字段的值计算索引数据的分布位置,路由字段的值相同的记录会被索引到相同的数据分区中。

index_sort 索引预排序设置,包含sorters设置。如果不设置,则默认按照主键排序。
说明 含有Nested类型的索引不支持index_sort,没有预排序。
sorters(必选):索引的预排序方式,支持按照主键排序和字段值排序。关于排序的更多信息,请参见 排序和翻页
  • PrimaryKeySort表示按照主键排序,包含如下设置:

    order:排序的顺序,可按升序或者降序排序,默认为升序(SortOrderConst::SORT_ORDER_ASC)。

  • FieldSort表示按照字段值排序,包含如下设置:

    只有建立索引且开启排序与统计聚合功能的字段才能进行预排序。

    • field_name:排序的字段名。
    • order:排序的顺序,可按照升序或者降序排序,默认为升序(SortOrderConst::SORT_ORDER_ASC)。
    • mode:当字段存在多个值时的排序方式。

示例

$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(//含有Nested类型的索引不支持index_sort,没有预排序。
//            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();