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
-
A Tablestore client is initialized. For more information, see Initialize a Tablestore client.
-
A data table is created with `max versions` set to 1 and `time to live` set to -1. For more information, see Create a data table.
Usage notes
The data types of fields in the search index must match the data types 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
Specify the table name (`table_name`), search index name (`index_name`), and index schema (`schema`). The schema includes `field_schemas` (field settings), `index_setting` (index settings), and `index_sort` (presorting settings).
|
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` includes the following parameters:
|
|
index_setting |
Index settings, including 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 index data is written, the system calculates the distribution location of the index data based on the values of the routing fields. Records with the same routing field values are indexed to the same data partition. |
|
index_sort |
Index presorting settings, including the `sorters` setting. Default: sorted by primary key. Note
Indexes that contain Nested type fields do not support `index_sort` and are not presorted. sorters (Required): The presorting method for the search index. Supports PrimaryKeySort and FieldSort. For more information, see Sorting and paging.
|
Example
Create a search index with the following columns: keyword (Keyword), text (Text), geo (Geo-point), long (Long), double (Double), boolean (Boolean), array (Keyword), and nested (Nested). The nested column contains a nested_keyword subcolumn of the Keyword type. The index is presorted by the primary key and 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,
'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
References
-
After creating a search index, use its query methods for multi-dimensional data queries. 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.
-
Apply sorting and pagination or collapsing (deduplication) to query results.
-
Manage search indexes as needed. For more information, see Dynamically modify schema, List search indexes, Query the description of a search index, and Delete a search index.
-
To perform data analysis such as finding maximum and minimum values, calculating sums, or counting rows, use the statistical aggregation feature or the SQL query feature.
-
To quickly export data without requiring a specific order, use the parallel scan feature.