使用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();