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
-
A Tablestore client is initialized. For more information, see Initialize a Tablestore client.
-
A data table is created and meets the following conditions. For more information, see Create a data table.
-
The maximum number of versions is set to 1.
-
The time to live (TTL) is set to -1, or the UpdateRow operation is disabled for the data table.
-
Usage notes
-
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, 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
To create a search index, 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).
|
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. During index data writes, the system distributes index data based on routing field values. Records with the same routing field values are stored in the same data partition. |
|
indexSort |
The index pre-sorting settings, which include the sorters setting. If not set, data is sorted by primary key by default. Note
Indexes that contain Nested fields do not support indexSort. No pre-sorting is performed. sorters (required): The pre-sorting method for the index. Supports sorting by primary key or by field value. For more information, see Sorting and paging.
|
|
timeToLive |
Optional. The time-to-live (TTL) of data, which is the data retention period. Unit: seconds. Default value: -1, which means data never expires. The minimum TTL value is 86400 seconds (one day). When the data retention period exceeds the TTL, expired data is automatically cleaned up. |
Examples
Create a search index and set a tokenizer
The following example creates a search index with these 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 has two sub-fields: sub_tag_name (Keyword) and tag_name (Keyword). The analyzer_single_word column uses single-word tokenization, analyzer_split uses delimiter tokenization, and analyzer_fuzzy 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 creates a search index with highlighting enabled. The index has three fields: k (Keyword), t (Text), and n (Nested). The n field has three sub-fields: nk (Keyword), nl (Long), and nt (Text). Highlighting 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
-
Differences between range queries using the GetRange and Search operations
-
Data cannot be found using the Search operation of a search index
-
Does Tablestore support queries similar to IN and BETWEEN...AND in relational databases?
-
The "field:xx must enable enable_sort_and_agg" error occurs when you use a search index
References
-
After creating a search index, select a query type for multi-dimensional data queries. Supported 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 querying data, apply sorting and paging, highlighting, or collapse (deduplication) operations on the result set.
-
After creating a search index, 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, use the statistical aggregation feature or the SQL query feature.
-
To quickly export data without requiring a specific order, use the parallel scan feature.