You can use the CreateSearchIndex method to create a search index for a data table. A data table can have multiple search indexes. When you create a search index, you must add the fields that you want to query to the index. You can also configure advanced options, such as custom routes and presorting.
Prerequisites
The Tablestore client is initialized. For more information, see Initialize a Tablestore client.
A data table is created. The `max versions` for the table must be 1, and the `time to live` must be -1. For more information, see Create a data table.
Usage notes
When you create a search index, the data types of the fields in the search index must match those of the corresponding fields in the data table.
API
/**
* Create a search index.
* @api
*
* @param [] $request
* The request parameters, such as the table name and index configuration.
* @return [] The response.
* @throws OTSClientException Thrown if a parameter check fails or the server returns a verification error.
* @throws OTSServerException Thrown if 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 (`table_name`), search index name (`index_name`), and index schema (`schema`). The schema includes `field_schemas` (settings for all index fields), `index_setting` (index settings), and `index_sort` (index presorting settings). 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 | A list of field schemas. Each `field_schema` contains the following parameters:
|
index_setting | Index settings, which include the `routing_fields` setting. routing_fields (Optional): Custom routing fields. You can select some primary key columns as routing fields. In most cases, you only need to set one. If you set multiple routing keys, the system concatenates their values into a single value. When you write data to the index, the system calculates the data distribution location based on the values of the routing fields. Records with the same routing field values are indexed into the same data partition. |
index_sort | Index presorting settings, which include the `sorters` setting. If this is not set, data is sorted by primary key by default. Note Indexes that contain Nested type fields do not support `index_sort` and are not presorted. sorters: required. This parameter specifies the presorting method for the search index. PrimaryKeySort and FieldSort are supported. For more information, see 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: a keyword column of the Keyword type, a text column of the Text type, a geo column of the Geo-point type, a long column of the Long type, a double column of the Double type, a boolean column of the Boolean type, an array column of the Keyword type, and a 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 is configured to never expire.
$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,
'is_array' => false
),
array(
'field_name' => 'text',
'field_type' => FieldTypeConst::TEXT,
'analyzer' => 'single_word',
'index' => true,
'enable_sort_and_agg' => false,
'is_array' => false
),
array(
'field_name' => 'geo',
'field_type' => FieldTypeConst::GEO_POINT,
'index' => true,
'enable_sort_and_agg' => true,
'is_array' => false
),
array(
'field_name' => 'long',
'field_type' => FieldTypeConst::LONG,
'index' => true,
'enable_sort_and_agg' => true,
'is_array' => false
),
array(
'field_name' => 'double',
'field_type' => FieldTypeConst::DOUBLE,
'index' => true,
'enable_sort_and_agg' => true,
'is_array' => false
),
array(
'field_name' => 'boolean',
'field_type' => FieldTypeConst::BOOLEAN,
'index' => true,
'enable_sort_and_agg' => false,
'is_array' => false
),
array(
'field_name' => 'array',
'field_type' => FieldTypeConst::KEYWORD,
'index' => true,
'enable_sort_and_agg' => false,
'is_array' => true
),
array(
'field_name' => 'nested',
'field_type' => FieldTypeConst::NESTED,
'index' => false,
'enable_sort_and_agg' => false,
'field_schemas' => array(
array(
'field_name' => 'nested_keyword',
'field_type' => FieldTypeConst::KEYWORD,
'index' => false,
'enable_sort_and_agg' => false,
'is_array' => false
)
)
),
),
'index_setting' => array(
'routing_fields' => array("pk1")
),
// "index_sort" => array(// Indexes that contain Nested type fields do not support index_sort and are not presorted.
// 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
Related documents
After you create a search index, you can use its query methods to query data across multiple dimensions. The supported query methods include 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 apply sorting and pagination or collapsing (removing duplicates) to the result set.
After you create a search index, you can manage it based on your requirements. For more information, see Dynamically modify schema, List search indexes, Query the description of a search index, and Delete a search index.
If you want to perform data analysis, such as finding the maximum and minimum values, calculating sums, or counting the number of rows, you can use the statistical aggregation feature or the SQL query feature.
If you want to quickly export data and the order of the entire result set is not important, you can use the parallel scan feature.