You can call the CreateSearchIndex operation to create one or more search indexes for a data table. When you create a search index, you can add the fields that you want to query to the search index and configure advanced settings for the search index. For example, you can configure the routing key and presorting settings.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
A data table that meets the following conditions is created. For more information, see Create a data table.
The max versions parameter is set to 1.
The time to live (TTL) is set to -1 or updates on the data table are prohibited.
Usage notes
The data types of the fields in a search index must match the data types of the fields in the data table for which the search index is created. For more information, see Data types.
To set the timeToLive parameter of a search index to a value other than -1, make sure that the UpdateRow operation is prohibited on the data table for which the search index is created. The value of the timeToLive parameter for the search index must be less than or equal to the value of the timeToLive parameter for the data table. For more information, see Specify the TTL of a search index.
Parameters
When you create a search index, you must configure the tableName, indexName, and schema parameters. You must also configure the fieldSchemas, indexSetting, and indexSort parameters in the schema parameter. The following table describes the preceding parameters.
Parameter | Description |
tableName | The name of the data table. |
indexName | The name of the search index. |
fieldSchemas | The list of field schemas. In each field schema, configure the following parameters:
|
indexSetting | The settings of the search index, including the settings of the routingFields parameter. routingFields (optional): the custom routing fields. You can specify specific primary key columns as routing fields. In most cases, you need to specify only one routing field. If you specify multiple routing fields, the system concatenates the values of the routing fields into one value as the partition key. Tablestore distributes data that is written to a search index across different partitions based on the specified routing fields. Data with the same routing field values is distributed to the same partition. |
indexSort | The presorting settings of the search index, including the settings of the sorters parameter. If you do not configure the indexSort parameter, field values are sorted by primary key. Note If you set the fieldType parameter to Nested, you cannot configure the indexSort parameter. sorters: This parameter is required and specifies the presorting method for the search index. Valid values: PrimaryKeySort and FieldSort. For more information, see Sorting and paging.
|
timeToLive | (Optional) The retention period of the data in the search index. Unit: seconds. Default value: -1. The value of this parameter must be greater than or equal to 86400. A value of 86400 specifies one day. You can also set this parameter to -1, which specifies that data never expires. If the retention period of data exceeds the value of the timeToLive parameter, the data expires. Tablestore automatically deletes the expired data. |
Examples
Create a search index with an analyzer type specified
The following sample code provides an example on how to create a search index with an analyzer type specified. In this example, the search index consists of the following fields: the pic_id field of the Keyword type, the count field of the Long type, the time_stamp field of the Long type, the pic_description field of the Text type, the col_vector field of the Vector type, the pos field of the Geo-point type, the pic_tag field of the Nested type, the date field of the Date type, the analyzer_single_word field of the Text type, the analyzer_split field of the Text type, and the analyzer_fuzzy field of the Text type. The pic_tag field consists of the sub_tag_name subfield of the Keyword type and the tag_name subfield of the Keyword type. The analyzer type is single-word tokenization for the analyzer_single_word field, delimiter tokenization for the analyzer_split field, and fuzzy tokenization for the analyzer_fuzzy field.
client.createSearchIndex({
tableName: "<TABLE_NAME>", // Specify the name of the data table.
indexName: "<INDEX_NAME>", // Specify the name of the search index.
schema: {
fieldSchemas: [
{
fieldName: "pic_id",
fieldType: TableStore.FieldType.KEYWORD, // Specify the name and type of the field.
index: true, // Enable indexing for the field.
enableSortAndAgg: true, // Enable sorting and aggregation for the field.
store: false,
isAnArray: false
},
{
fieldName: "count",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false
},
{
fieldName: "time_stamp",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
},
{
fieldName: "pic_description",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
},
{
fieldName: "col_vector",
fieldType: TableStore.FieldType.VECTOR,
index: true,
isAnArray: false,
vectorOptions: {
dataType: TableStore.VectorDataType.VD_FLOAT_32,
dimension: 4,
metricType: TableStore.VectorMetricType.VM_COSINE,
}
},
{
fieldName: "pos",
fieldType: TableStore.FieldType.GEO_POINT,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false,
},
{
fieldName: "pic_tag",
fieldType: TableStore.FieldType.NESTED,
index: false,
enableSortAndAgg: false,
store: false,
fieldSchemas: [
{
fieldName: "sub_tag_name",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: false,
},
{
fieldName: "tag_name",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: false,
}
]
},
{
fieldName: "date",
fieldType: TableStore.FieldType.DATE,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false,
dateFormats: ["yyyy-MM-dd'T'HH:mm:ss.SSSSSS"],
},
{
fieldName: "analyzer_single_word",
fieldType: TableStore.FieldType.TEXT,
analyzer: "single_word",
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzerParameter: {
caseSensitive: true,
delimitWord: false,
}
},
{
fieldName: "analyzer_split",
fieldType: TableStore.FieldType.TEXT,
analyzer: "split",
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzerParameter: {
delimiter: ",",
}
},
{
fieldName: "analyzer_fuzzy",
fieldType: TableStore.FieldType.TEXT,
analyzer: "fuzzy",
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzerParameter: {
minChars: 1,
maxChars: 5,
}
},
],
indexSetting: { // Configure the settings of the search index.
"routingFields": ["count", "pic_id"], // You can specify only the primary key columns of the data table as the routing fields of the search index.
"routingPartitionSize": null
},
//indexSort: {// You can skip the presorting settings for search indexes that contain Nested fields.
//sorters: [
// { // If you do not configure the indexSort parameter, data is sorted by primary key in ascending order.
// primaryKeySort: {
// order: TableStore.SortOrder.SORT_ORDER_ASC
// }
// },
//{
// fieldSort: {
// fieldName: "Col_Keyword",
// order: TableStore.SortOrder.SORT_ORDER_DESC // Specify the index sort order.
// }
//}
//]
//},
timeToLive: 1000000, // Specify the data retention period. Unit: seconds.
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:',data);
});
Create a search index with the highlight feature enabled
The following sample code provides an example on how to create a search index with the highlight feature enabled. In this example, the search index consists of the following fields: the k field of the Keyword type, the t field of the Text type, and the n field of the Nested type. The n field consists of the following subfields: the nk subfield of the Keyword type, the nk subfield of the Long type, and the nt subfield of the Text type. In addition, the highlight feature is enabled for the t field of the Text type and the nt subfield of the Text type.
client.createSearchIndex({
tableName: "<TABLE_NAME>", // Specify the name of the data table.
indexName: "<SEARCH_INDEX_NAME>", // Specify the name of the search index.
schema: {
fieldSchemas: [
{
fieldName: "k",
fieldType: TableStore.FieldType.KEYWORD, // Specify the name and type of the field.
index: true, // Enable indexing for the field.
enableSortAndAgg: true, // Enable sorting and aggregation for the field.
store: false,
isAnArray: false
},
{
fieldName: "t",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
enableHighlighting: true, // Enable highlight for the field.
store: true,
isAnArray: false,
},
{
fieldName: "n",
fieldType: TableStore.FieldType.NESTED,
index: false,
enableSortAndAgg: false,
store: false,
fieldSchemas: [
{
fieldName: "nk",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
store: false,
},
{
fieldName: "nl",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: true,
store: false,
},
{
fieldName: "nt",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
enableHighlighting: true, // Enable highlight for the field.
store: false,
},
]
},
],
indexSetting: { // Configure the settings of the search index.
"routingFields": ["id"], // You can specify only the primary key columns of the data table as the routing fields of the search index.
"routingPartitionSize": null
},
//indexSort: {// You can skip the presorting settings for search indexes that contain Nested fields.
//sorters: [
// { // If you do not configure the indexSort parameter, data is sorted by primary key in ascending order.
// primaryKeySort: {
// order: TableStore.SortOrder.SORT_ORDER_ASC
// }
// },
//{
// fieldSort: {
// fieldName: "Col_Keyword",
// order: TableStore.SortOrder.SORT_ORDER_DESC // Specify the index sort order.
// }
//}
//]
//},
timeToLive: 1000000, // Specify the data retention period. Unit: seconds.
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:',data);
});
FAQ
References
After you create a search index, you can use the query methods provided by the search index to query data from multiple dimensions based on your business requirements. A search index usually provides the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, geo query, Boolean query, KNN vector query, nested query, and exists query.
When you query data, you can perform sorting and paging on the result set, use the highlight feature to highlight the keywords in the result set, or use the collapse (distinct) feature to collapse the result set based on a specific field. For more information, see Sorting and paging, Highlight, and Collapse (distinct).
After you create a search index, you can manage the search index based on your business requirements. For example, you can dynamically modify the schema of the search index, update the configurations of the search index, query the names of search indexes in an instance, query information of the search index, and delete the search index. For more information, see Dynamically modify the schema of a search index, Update the configurations of a search index, List search indexes, Query the description of a search index, and Delete a search index.
You can use the aggregation feature or the SQL query feature to analyze data in a table. For example, you can query the maximum and minimum values, the sum of the values, and the number of rows. For more information, see Aggregation and SQL query.
If you want to obtain all rows that meet the query conditions without the need to sort the rows, you can use the parallel scan feature. For more information, see Parallel scan.