You can call the CreateSearchIndex operation to create one or more search indexes for a data table. When you create a search index, you can add the fields that you want to query to the search index and configure advanced settings for the search index. For example, you can configure the routing key and presorting settings.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
A data table for which the max versions parameter is set to 1 and time to live (TTL) is set to -1 is created. For more information, see Create a data table.
Usage notes
The data types of the fields in a search index must match the data types of the fields in the data table for which the search index is created. For more information, see Data type mappings.
API 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 thrown when a parameter error occurs or the Tablestore server returns a verification error.
* @throws OTSServerException The exception that is thrown 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 the table_name, index_name, and schema parameters. In the schema parameter, specify the field_schemas, index_setting, and index_sort parameters. The following table describes the parameters.
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 contains the following parameters:
|
index_setting | The settings of the search index, including the routing_fields parameter. routing_fields: optional. This parameter specifies custom routing fields. You can specify multiple primary key columns as routing fields. In most cases, you need to specify only one routing field. If you specify multiple routing fields, the system concatenates the values of the routing fields into one value as the partition key. Tablestore distributes data that is written to a search index across different partitions based on the specified routing fields. The data that has the same routing field values is distributed to the same partition. |
index_sort | The presorting settings of the search index, including the sorters parameter. If you do not specify the index_sort parameter, field values are sorted by primary key. Note You can skip the presorting settings for search indexes that contain fields of the Nested type. sorters: required. This parameter specifies the presorting method for the search index. PrimaryKeySort and FieldSort are supported. For more information, see Perform sorting and paging.
|
Example
The following sample code shows how to create a search index. In this example, the search index consists of the following columns: the keyword column of the Keyword type, the text column of the Text type, the geo column of the Geo-point type, the long column of the Long type, the double column of the Double type, the boolean column of the Boolean type, the array column of the Keyword type, and the nested column of the Nested type. The nested column contains a nested_keyword subcolumn of the Keyword type. The data in the search index is presorted based on the primary key of the data table and data in the search index 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,
'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 fields of the Nested 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($request);
FAQ
References
After you create a search index, you can use the query methods provided by the search index to query data from multiple dimensions based on your business requirements. The following query methods are supported: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, geo query, Boolean query, nested query, and exists query.
When you query data, you can perform sorting and paging on the result set or use the collapse (distinct) feature to collapse the result set based on a specific field. For more information, see Sorting and paging and Collapse (distinct).
After you create a search index, you can manage the search index based on your business requirements. For example, you can dynamically modify the schema of the search index, query the names of search indexes in an instance, query information of the search index, and delete the search index. For more information, see Dynamically modify the schema of a search index, List search indexes, Query the description of a search index, and Delete a search index.
You can use the aggregation feature or the SQL query feature to analyze data in a table. For example, you can query the maximum and minimum values, the sum of the values, and the number of rows. For more information, see Aggregation and SQL query.
If you want to obtain all rows that meet the query conditions without the need to sort the rows, you can use the parallel scan feature. For more information, see Parallel scan.