After a data table is created, you can use Tablestore CLI to create a search index, obtain a list of search indexes, query information about a search index, use a search index to query, and delete search indexes.
Create a search index
You can create a search index.
Syntax
create_search_index -n search_index_name
Parameter description:
Parameter | Required | Example | Description |
-i, --input | No | /tmp/indexschema.json | The JSON file that is used to configure the search index schema. |
-n, --name | Yes | search_index | The name of the search index. |
-t,--table | No | mytable | The name of the data table. |
--ttl | No | -1 | The retention period of data. The system deletes data based on a Time-to-live (TTL) mechanism. If the retention period exceeds the TTL, Tablestore automatically deletes the expired data. Default value: -1. Unit: seconds. A value of -1 indicates that data never expires. If you want to specify a data TTL, the value must be equal to or greater than 86400 seconds (one day). Important To use the TTL feature, you must set Allow Updates to No for the data table in which the search index is created. The TTL value of a search index is independent of and must be smaller than or equal to the TTL value of the data table for which the search index is created. For more information, see Specify the TTL of a search index. |
Examples
Create a search index named search_index:
create_search_index -n search_index
Enter the index schema as prompted:
{
"IndexSetting": {
"RoutingFields": null
},
"FieldSchemas": [
{
"FieldName": "gid",
"FieldType": "LONG",
"Index": true,
"EnableSortAndAgg": true,
"Store": true,
"IsArray": false,
"IsVirtualField": false
},
{
"FieldName": "uid",
"FieldType": "LONG",
"Index": true,
"EnableSortAndAgg": true,
"Store": true,
"IsArray": false,
"IsVirtualField": false
},
{
"FieldName": "col2",
"FieldType": "LONG",
"Index": true,
"EnableSortAndAgg": true,
"Store": true,
"IsArray": false,
"IsVirtualField": false
},
{
"FieldName": "col3",
"FieldType": "TEXT",
"Index": true,
"Analyzer": "single_word",
"AnalyzerParameter": {
"CaseSensitive": true,
"DelimitWord": null
},
"EnableSortAndAgg": false,
"Store": true,
"IsArray": false,
"IsVirtualField": false
},
{
"FieldName": "col1",
"FieldType": "KEYWORD",
"Index": true,
"EnableSortAndAgg": true,
"Store": true,
"IsArray": false,
"IsVirtualField": false
},
{
"FieldName": "col3V",
"FieldType": "LONG",
"Index": true,
"EnableSortAndAgg": true,
"Store": true,
"IsArray": false,
"IsVirtualField": true,
"SourceFieldNames": [
"col3"
]
}
]
}
The following table describes the parameters of the search index schema.
Parameter | Required | Description |
IndexSetting | No | The setting of the search index, which includes the following parameter: RoutingFields (optional): The custom routing fields. You can specify some primary key columns as routing fields. Tablestore distributes data that is written to a search index across different partitions based on the specified routing fields. The data records whose routing field values are the same is distributed to the same partition. |
FieldSchemas | Yes | The list of field schemas. You can configure the following parameters for each field schema:
|
IndexSort | No | The presorting setting for the search index, which includes the Sorters parameter. If no value is specified for the IndexSort parameter, field values are sorted based on primary key by default. Important You can skip the presorting settings for the search indexes that contain fields of the Nested type. Sorters (optional): The indexing pre-sorting methods. It includes the Name and Sorter parameters. You can use the PrimaryKeySort and FieldSort methods. For more information, see Perform sorting and paging.
|
Obtain a list of search indexes
You can obtain a list of search indexes that are created for the current data table.
Syntax
list_search_index
Parameter description:
Parameter | Required | Example | Description |
-a, --all | No | N/A | Specifies that the search indexes of all data tables are displayed. |
-d, --detail | No | N/A | Specifies that the details about the search indexes are displayed. |
-t,--table | No | mytable | The name of the data table. |
Examples
Obtain a list of the search indexes of the current data table:
list_search_index
Sample result:
+-----------+--------------+
| TableName | IndexName |
+-----------+--------------+
| mytable | search_index |
+-----------+--------------+
Query information about a search index
You can query information about a search index.
Syntax
describe_search_index -n search_index_name
Parameter description:
Parameter | Required | Example | Description |
-n, --name | Yes | search_index | The name of the search index. |
-o, --output | No | /tmp/describeinfo.json | Export query results to a JSON or TXT format file on your computer. |
-t,--table | No | mytable | The name of the data table. |
Examples
Query information about the search index named search_index:
describe_search_index -n search_index
The Index schema
parameter in the returned results describes the search index schema, and TimeToLive indicates the data retention period for the search index.
Query data by using a search index
You can query data that meets specified conditions by using a search index and then perform aggregation operations on the returned results.
Syntax
search -n search_index_name --return_all_indexed
Parameter description:
Parameter | Required | Example | Description |
-c, --column | No | col1,col2 | The columns to return. Separate multiple columns with commas (,). |
-n, --name | Yes | search_index | The name of the search index. |
-f, --print_format | No | table | The format in which the output results are printed. Default value: table. |
--return_all | No | N/A | Specifies to return all columns. |
--return_all_indexed | No | N/A | Specifies to return all columns that are indexed in the search indexes. |
-t,--table | No | mytable | The name of the data table. |
Examples
Query data by using the search index named search_index and return all columns for which indexing is enabled:
search -n search_index --return_all_indexed
Enter the query conditions as prompted by the system:
{
"Offset": -1,
"Limit": 10,
"Collapse": null,
"Sort": null,
"GetTotalCount": true,
"Token": null,
"Query": {
"Name": "BoolQuery",
"Query": {
"MinimumShouldMatch": null,
"MustQueries": null,
"MustNotQueries": null,
"FilterQueries": null,
"ShouldQueries": [{
"Name": "RangeQuery",
"Query": {
"FieldName": "gid",
"From": null,
"To": 10,
"IncludeLower": false,
"IncludeUpper": false
}
}, {
"Name": "TermQuery",
"Query": {
"FieldName": "gid",
"Term": "77"
}
}]
}
},
"Aggregations": [{
"Name": "avg",
"Aggregation": {
"AggName": "agg1",
"Field": "gid",
"MissingValue": null
}
}]
}
Parameter description:
Parameter | Required | Description |
Offset | No | The position from which the current query starts. |
Limit | No | The maximum number of rows that you want the current query to return. If you want the current query to return multiple random rows of data, set limit to a positive integer. To query only the number of rows that meet the query conditions without returning specific data, set limit to 0. |
Collapse | No | The configuration of the collapse parameter. It can be used to collapse the result set of a specified column, so that data of the specified types appears only once in the result set. This ensures diversity in the result set. For more information, see Collapse (distinct). FieldName: The name of the column based on which the result set is collapsed. Only columns whose values are of the INTEGER, FLOATING-POINT, and KEYWORD data types are supported. |
Sort | No | The sorting method of the results. For more information, see the "Specify a sorting method when you query data" section of Sorting and paging. If a sorting method is not specified in the query, the query results are returned by the presorting method. If the presorting method is not specified when the search index is created, the query results are sorted by the primary key. |
GetTotalCount | No | Specifies whether to return the total number of rows that meet the query conditions. The default value of this parameter is false, which indicates that the total number of rows that meet the query conditions is not returned. If this parameter is set to true, the query performance is compromised. To obtain the total number of matched rows, set GetTotalCount to true. |
Token | No | If Tablestore cannot read all data that meets the query conditions, Tablestore returns Token. You can use the Token value to continue to read the subsequent data. When you read data for the first time, set the token value to null. |
Query | Yes | The query method. The following query methods are supported: match all query (MatchAllQuery), match query (MatchQuery), match phrase query (MatchPhraseQuery), term query (TermQuery), terms query (TermsQuery), and prefix query (PrefixQuery). For more information, see Use Tablestore SDKs. |
Aggregations | No | The configuration of aggregation operations. You can perform aggregation operations to obtain the minimum value, maximum value, sum, average, and count and distinct count of rows. For more information, see Aggregation.
|
Delete a search index
You can delete a search index that you no longer need.
Syntax
drop_search_index -n search_index_name -y
Parameter description:
Parameter | Required | Example | Description |
-n, --name | Yes | search_index | The name of the search index. |
-t,--table | No | mytable | The name of the data table. |
-y, --yes | Yes | N/A | Specifies that the confirmation information is displayed. This parameter is required. |
Examples
Delete the search index named search_index:
drop_search_index -n search_index -y