Search indexes support fields of the Array type in addition to fields of the basic type, such as the Long, Double, Boolean, Keyword, and Text types. You can use a field of the Array type to store elements of the basic type. If you specify an ARRAY_EXTRACT condition as a WHERE clause in a SELECT statement, you can execute the SELECT statement to query fields of the Array type in a search index. This topic describes how to execute SQL statements to query fields of the Array type in a search index.
For more information about the Array type, see ARRAY.
Usage notes
You cannot execute SQL statements to query fields of the
Geo-point
type in a search index.You can specify Array as the type of a field in a search index only if the column to which the field is mapped in the data table is of the String type. When you create the search index, you must set the type of the field to the actual type of elements that you want to use an array to store and enable the Array attribute for the field.
Data type mappings
Data type in data tables | Data type in search indexes | Data type in SQL |
String | The type of the field is the same as the actual type of elements that you want to use an array to store. Array is turned on for the field in the Tablestore console or the IsArray parameter is set to true for the field by using Tablestore SDKs. |
|
Prerequisites
A data table is created and data is written to the data table. For more information, see Operations on tables and Write data.
Procedure
If you want to query fields of the Array type by executing SQL statements, you must create a search index for a data table, create a mapping table for the search index, and then execute SQL statements to query fields of the Array type in the search index.
Step 1: Create a search index
To store elements by using a field of the Array type, when you create a search index, you must turn on Array for the field in the Tablestore console or set the IsArray parameter to true for the field by using Tablestore SDKs. For more information, see Create a search index.
Step 2: Create a mapping table for the search index
To query a field of the Array type in a search index by executing SQL statements, you must create a mapping table for the search index. For more information about how to create a mapping table for a search index, see Create mapping tables for search indexes.
In the CREATE TABLE
statement, you must specify a valid name and the mapped data type in SQL for each field of the Array type. When you create a mapping table for a search index, we recommend that you specify the MEDIUMTEXT type as the mapped data type in SQL for a field of the Array type.
The following sample SQL statement provides an example on how to create a mapping table for a search index that contains fields of the Array type:
CREATE TABLE `test_table__test_table_index`(
`col_keyword_array` MEDIUMTEXT,
`col_long_array` MEDIUMTEXT
)
ENGINE='searchindex'
ENGINE_ATTRIBUTE='{"index_name":"test_table_index", "table_name":"test_table"}';
Step 3: Execute SQL statements to query data by using the search index
To query a field of the Array type by executing SQL statements, you must use the ARRAY_EXTRACT(col_name)
function. In this function, col_name
specifies the name of a field of the Array type. You can use the function together with an operator as a query condition. Example: ARRAY_EXTRACT(col_long_array) = 1
.
When you execute SQL statements to query a field of the Array type, you cannot use the field of the Array type together with an operator.
For example, a search index contains a field of the Array type named col_long_array, the type of the column to which the field is mapped in the data table is String, the field of the Array type stores elements of the Long type. You can execute the following sample SQL statement to query the rows in which the col_long_array field of the Array type contains an element whose value is 1:
SELECT * FROM `test_table__test_table_index` WHERE ARRAY_EXTRACT(col_long_array) = 1;
Limits
You can use the
ARRAY_EXTRACT(col_name)
function as a condition to query a field of the Array type only when you execute SQL statements by using a mapping table that is created for the search index. You can specify only one field of the Array type in the function. The function can be used as a WHERE clause of a SELECT statement but cannot be used as a column expression of the SELECT statement, as an aggregate function, or for sorting.A field of the Array type can be used as the field or column expression in a SELECT statement but cannot be used as an aggregate function or for sorting.
If you use the
ARRAY_EXTRACT(col_name)
function together with an operator as a query condition, you cannot perform calculation after the type of elements in the field of the Array type is converted. Make sure that the type of elements in the field of the Array type is valid. For example, the col_long_array field is of the Array type and stores elements of the Long type. You can usearray_extract(col_long_array) = 1
as a query condition to perform calculation. However, you cannot usearray_extract(col_long_array) = '1'
as a query condition to perform calculation.If the type of elements in the field of the Array type is
Text
, you must use the ARRAY_EXTRACT(col_name) function together with thetext_match()
ortext_match_phrase()
function provided by full-text search. Example:text_match(array_extract(col_text), "xxx")
. For information about full-text search, see Full-text search.
Examples
In this example, a data table named test_table
is used. The data table contains the col_keyword_array
column of the String type and the col_long_array
column of the String type.
Create a search index. For more information, see Use the Tablestore console and Use Tablestore SDKs.
In this example, a search index named
test_table_index
is created. The search index contains two fields of the Array type: thecol_keyword_array
field that stores elements of the Keyword type and thecol_long_array
field that stores elements of the Long type.The following figure shows how to create a search index in the Tablestore console.
Create a mapping table for the search index. For more information, see Create mapping tables for search indexes.
In this example, a mapping table named
test_table__test_table_index
is created for the search index. In the mapping table, the type of thecol_keyword_array
andcol_long_array
fields isMEDIUMTEXT
.You can execute the following sample SQL statement to create the mapping table:
CREATE TABLE `test_table__test_table_index`( `col_keyword_array` MEDIUMTEXT, `col_long_array` MEDIUMTEXT ) ENGINE='searchindex' ENGINE_ATTRIBUTE='{"index_name":"test_table_index", "table_name":"test_table"}';
After you create a mapping table for the search index, execute the following statement to query data from the mapping table:
SELECT * FROM `test_table__test_table_index`;
The following figure shows the query result. In this example, five data records are contained in the
test_table__test_table_index
mapping table.Execute the SELECT statement to query data.
In this example, the SELECT statement is executed to query the rows in which the
col_long_array
field of the Array type contains an element whose value is 1 or the rows in which thecol_keyword_array
field of the Array type contains an element whose value starts with "d".SELECT * FROM test_table__test_table_index WHERE ARRAY_EXTRACT(col_long_array) = 1 OR ARRAY_EXTRACT(col_keyword_array) like 'd%';
The following figure shows the query result.