You can use the CreateSearchIndex method to create one or more search indexes for a data table. 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 routing fields and pre-sorting.
Prerequisites
Initialize a Tablestore client. For more information, see Initialize a Tablestore client.
Create a data table that meets the following conditions. For more information, see Create a data table.
The max versions must be 1.
The time to live (TTL) must be -1 or updates on the data table must be disabled.
Usage notes
When you create a search index, the data types of the fields in the search index must match the data types of the fields in the data table.
To set the TTL of a search index to a value other than -1, you must disable the UpdateRow operation for the data table. The TTL of the search index must be less than or equal to the TTL of the data table. For more information, see Lifecycle management.
Parameters
When you create a search index, you must specify the table name (tableName), index name (indexName), and index schema (schema). The schema includes field schemas (fieldSchemas), index settings (indexSetting), and index pre-sorting settings (indexSort). The following table describes these parameters.
Parameter | Description |
tableName | The name of the data table. |
indexName | The name of the search index. |
fieldSchemas | The list of field schemas. Each field schema contains the following parameters:
|
indexSetting | The index settings, which include the routingFields setting. routingFields (optional): The 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 the values of the routing keys into a single value. When you write data to the index, 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. |
indexSort | The index pre-sorting settings, which include the sorters setting. If you do not set this parameter, the data is sorted by primary key by default. Note indexSort is not supported for indexes that contain Nested fields. No pre-sorting is performed. sorters (required): The pre-sorting method for the index. You can sort by primary key or by field value. For more information about sorting, see Sorting and paging.
|
timeToLive | (Optional) The time to live (TTL), which is the data retention period. Unit: seconds. The default value is -1, which indicates that the data never expires. The minimum value for TTL is 86,400 seconds (one day). You can also set it to -1. When the data retention period exceeds the specified TTL, the system automatically deletes the expired data. |
Examples
Create a search index and set a tokenizer
The following example shows how to create a search index. The index includes the following columns: pic_id (Keyword), count (Long), time_stamp (Long), pic_description (Text), col_vector (Vector), pos (Geo-point), pic_tag (Nested), date (Date), analyzer_single_word (Text), analyzer_split (Text), and analyzer_fuzzy (Text). The pic_tag column includes two sub-fields: sub_tag_name (Keyword) and tag_name (Keyword). The analyzer_single_word column uses single-word tokenization. The analyzer_split column uses delimiter tokenization. The analyzer_fuzzy column uses fuzzy tokenization.
client.createSearchIndex({
tableName: "<TABLE_NAME>", // Set the table name.
indexName: "<INDEX_NAME>", // Set the search index name.
schema: {
fieldSchemas: [
{
fieldName: "pic_id",
fieldType: TableStore.FieldType.KEYWORD, // Set the field name and field type.
index: true, // Enable indexing.
enableSortAndAgg: true, // Enable sorting and aggregation.
isAnArray: false
},
{
fieldName: "count",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: true,
isAnArray: false
},
{
fieldName: "time_stamp",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: false,
isAnArray: false,
},
{
fieldName: "pic_description",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
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,
isAnArray: false,
},
{
fieldName: "pic_tag",
fieldType: TableStore.FieldType.NESTED,
index: false,
enableSortAndAgg: false,
fieldSchemas: [
{
fieldName: "sub_tag_name",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
},
{
fieldName: "tag_name",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
}
]
},
{
fieldName: "date",
fieldType: TableStore.FieldType.DATE,
index: true,
enableSortAndAgg: 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,
isAnArray: false,
analyzerParameter: {
caseSensitive: true,
delimitWord: false,
}
},
{
fieldName: "analyzer_split",
fieldType: TableStore.FieldType.TEXT,
analyzer: "split",
index: true,
enableSortAndAgg: false,
isAnArray: false,
analyzerParameter: {
delimiter: ",",
}
},
{
fieldName: "analyzer_fuzzy",
fieldType: TableStore.FieldType.TEXT,
analyzer: "fuzzy",
index: true,
enableSortAndAgg: false,
isAnArray: false,
analyzerParameter: {
minChars: 1,
maxChars: 5,
}
},
],
indexSetting: { // The configuration options of the index.
"routingFields": ["count", "pic_id"], // Only primary key columns can be set as routing fields.
"routingPartitionSize": null
},
//indexSort: {// indexSort is not supported for indexes that contain Nested fields. No pre-sorting is performed.
//sorters: [
// { // If you do not set indexSort, the data is sorted by primary key in ascending order by default.
// primaryKeySort: {
// order: TableStore.SortOrder.SORT_ORDER_ASC
// }
// },
//{
// fieldSort: {
// fieldName: "Col_Keyword",
// order: TableStore.SortOrder.SORT_ORDER_DESC // Set the sorting order for indexSort.
// }
//}
//]
//},
timeToLive: 1000000, // Unit: seconds.
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:',data);
});Create a search index and enable highlighting
The following example shows how to enable highlighting when you create a search index. The index includes three fields: k (Keyword), t (Text), and n (Nested). The n field includes three sub-fields: nk (Keyword), nl (Long), and nt (Text). The highlighting feature is enabled for the t field and the nt sub-field.
client.createSearchIndex({
tableName: "<TABLE_NAME>", // Set the table name.
indexName: "<SEARCH_INDEX_NAME>", // Set the search index name.
schema: {
fieldSchemas: [
{
fieldName: "k",
fieldType: TableStore.FieldType.KEYWORD, // Set the field name and field type.
index: true, // Enable indexing.
enableSortAndAgg: true, // Enable sorting and aggregation.
isAnArray: false
},
{
fieldName: "t",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
enableHighlighting: true, // Enable highlighting for the field.
isAnArray: false,
},
{
fieldName: "n",
fieldType: TableStore.FieldType.NESTED,
index: false,
enableSortAndAgg: false,
fieldSchemas: [
{
fieldName: "nk",
fieldType: TableStore.FieldType.KEYWORD,
index: true,
enableSortAndAgg: true,
},
{
fieldName: "nl",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: true,
},
{
fieldName: "nt",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
enableHighlighting: true, // Enable highlighting for the field.
},
]
},
],
indexSetting: { // The configuration options of the index.
"routingFields": ["id"], // Only primary key columns can be set as routing fields.
"routingPartitionSize": null
},
//indexSort: {// indexSort is not supported for indexes that contain Nested fields. No pre-sorting is performed.
//sorters: [
// { // If you do not set indexSort, the data is sorted by primary key in ascending order by default.
// primaryKeySort: {
// order: TableStore.SortOrder.SORT_ORDER_ASC
// }
// },
//{
// fieldSort: {
// fieldName: "Col_Keyword",
// order: TableStore.SortOrder.SORT_ORDER_DESC // Set the sorting order for indexSort.
// }
//}
//]
//},
timeToLive: 1000000, // 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 select a query type to perform multi-dimensional data queries. Search index query types include term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, geo query, Boolean query, vector search, nested query, and exists query.
When you query data, you can perform sorting and paging, highlighting, or collapse (deduplication) operations on the result set.
After you create a search index, you can manage it as needed. Operations include dynamically modifying the schema, updating the search index configuration, listing search indexes, querying search index descriptions, and deleting a search index.
To perform data analytics, such as finding the maximum or minimum value, calculating a sum, or counting rows, you can use the statistical aggregation feature or the SQL query feature.
To quickly export data regardless of the order of the entire result set, you can use the parallel scan feature.