You can call the CreateSearchIndex operation to create one or more search indexes for a data table.
Prerequisites
- OTSClient is initialized. For more information, see Initialization.
- A data table whose timeToLive is set to -1 and maxVersions is set to 1 is created.
Parameters
When you create a search index, you must specify tableName, indexName, and schema. You must specify fieldSchemas, indexSetting, and indexSort in schema. The following table describes the parameters.
Parameter | Description |
---|---|
tableName | The name of the table. |
indexName | The name of the search index. |
fieldSchemas | The list of field schemas. You can configure the following parameters for each field
schema:
|
indexSetting | The settings of the search index, including routingFields.
routingFields: optional. This parameter specifies custom routing fields. You can specify some primary key columns as routing fields. Tablestore distributes data that is written to a search index to different partitions based on the specified routing fields. The data with the same routing field values is distributed to the same partition. |
indexSort | The presorting settings of the search index, including sorters. If indexSort is left
empty, data is sorted by primary key.
Note You can skip the presorting settings for search indexes that contain the Nested field
type.
sorters: required. This parameter specifies the presorting method for the search index.
PrimaryKeySort and FieldSort are supported. For more information about sorting, see
Sorting and pagination.
|
Examples
/**
*Create a search index that contains columns Col_Keyword, Col_Long, Col_Text, and Col_Nested.
*Set the type of data in Col_Keyword to Keyword, in Col_Long to Long, in Col_Text to Text, and in Col_Nested to Nested.
*/
client.createSearchIndex({
tableName: TABLE_NAME, // Set the name of the table.
indexName: INDEX_NAME, // Set the name of the search index.
schema: {
fieldSchemas: [
{
fieldName: "Col_Keyword",
fieldType: TableStore.FieldType.KEYWORD, // Set the name and type of the field.
index: true, // Enable the indexing feature.
enableSortAndAgg: true, // Enable the sorting and aggregation features.
store: false,
isAnArray: false
},
{
fieldName: "Col_Long",
fieldType: TableStore.FieldType.LONG,
index: true,
enableSortAndAgg: true,
store: true,
isAnArray: false
},
{
fieldName: "Col_Text",
fieldType: TableStore.FieldType.TEXT,
index: true,
enableSortAndAgg: false,
store: true,
isAnArray: false,
analyzer: "single_word"
},
// {
// fieldName: "Col_Nested",
// fieldType: TableStore.FieldType.NESTED,
// index: false,
// enableSortAndAgg: false,
// store: false,
// fieldSchemas: [ // The subfield indexes set in Nested fields.
// {
// fieldName: "Sub_Col_KeyWord",
// fieldType: TableStore.FieldType.KEYWORD,
// index: true,
// enableSortAndAgg: true,
// store: false
// },
// {
// fieldName: "Sub_Col_Long",
// fieldType: TableStore.FieldType.LONG,
// index: true,
// enableSortAndAgg: true,
// store: false
// }
// ]
// }
],
indexSetting: { // The configuration options of the index.
"routingFields": ["Pk_Keyword"], // You can only set the primary key column to routingFields.
"routingPartitionSize": null
},
indexSort: {// The search indexes that contain Nested fields do not support indexSort.
sorters: [
// { // If indexSort is not set, 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 index sorting order.
}
}
]
}
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});