使用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包含如下内容:
|
index_setting | 索引设置,包含routing_fields设置。 routing_fields(可选):自定义路由字段。可以选择部分主键列作为路由字段,在进行索引数据写入时,会根据路由字段的值计算索引数据的分布位置,路由字段的值相同的记录会被索引到相同的数据分区中。 |
index_sort | 索引预排序设置,包含sorters设置。如果不设置,则默认按照主键排序。
说明 含有Nested类型的索引不支持index_sort,没有预排序。
sorters(必选):索引的预排序方式,支持按照主键排序和字段值排序。关于排序的更多信息,请参见
排序和翻页。
|
示例
$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();