You can execute the CREATE TABLE statement to create a mapping table for an existing table or search index. This topic describes how to create a mapping table for an existing search index.
Background information
Indexes of different types may be created for a data table. When you execute the CREATE TABLE statement to create a mapping table for the data table, and the mapping table is used to query data, the SQL engine automatically selects a data table index, secondary index, or search index to meet your business requirements. You can execute the CREATE TABLE statement in SQL to create a mapping table for a specified search index so that you can select the specified search index to perform the query.
Syntax
CREATE TABLE [IF NOT EXISTS] user_defined_name(column_name data_type [,column_name data_type])
ENGINE='searchindex',
ENGINE_ATTRIBUTE='{"index_name": index_name, "table_name": table_name}';
Parameters
Parameter | Required | Description |
---|---|---|
IF NOT EXISTS | No | Specifies whether a success response is returned. If you specify IF NOT EXISTS, a success response is returned regardless of whether the table exists. Otherwise, a success response is returned only when the table does not exist. |
user_defined_name | Yes | The name of the mapping table for the search index. The name is used to identify the
mapping table in SQL.
The name is used for SQL operations. |
column_name | Yes | The name of the column.
The column name in SQL must be equivalent to the column name in the Tablestore table. For example, if the column name in the Tablestore table is Aa, the column name in SQL must be Aa, AA, aA, or aa. |
data_type | Yes | The data type of the column, such as BIGINT, DOUBLE, or BOOL.
The data type of the column in SQL must match the data type of the column in the Tablestore table. For more information about data type mappings, see Data type mappings. |
ENGINE | Yes | The execution engine that is used when you use the mapping table to query data. Default
value: tablestore. Valid values:
|
ENGINE_ATTRIBUTE | Yes | The attribute of the execution engine. The value of this parameter is in the JSON
format and includes the following items:
|
Examples
Create a mapping table named search_exampletable1 for the exampletable1_index search index that is created for the exampletable1 data table. The mapping table contains the id, colvalue, and content columns. The id column is of the BIGINT type, and the colvalue and content columns are of the MEDIUMTEXT type.
CREATE TABLE search_exampletable1(id BIGINT, colvalue MEDIUMTEXT, content MEDIUMTEXT) ENGINE='searchindex' ENGINE_ATTRIBUTE='{"index_name": "exampletable1_index", "table_name": "exampletable1"}';
- Query information about the search_exampletable1 mapping table.
SHOW INDEX IN search_exampletable1;
For more information about how to query index information about a table, see Query the index information about a table.
- Use the search_exampletable1 mapping table to query the rows in which the value of
the content column matches at least one of the tokens that are obtained by tokenizing
the "tablestore cool" string. Specify that up to 10 rows are returned and the id and
content columns are returned in each row that meets the query conditions.
SELECT id,content FROM search_exampletable1 WHERE TEXT_MATCH(content, "tablestore cool") LIMIT 10;
For more information about how to query data that matches the specified string, see Query data and Full-text search.