You can call the CreateSearchIndex operation to create one or more search indexes for a table.
Prerequisites
- The OTSClient instance is initialized. For more information, see Initialization.
- A table is created for which TimeToLive is set to -1 and MaxVersions is set to 1.
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. You use this parameter to customize routing fields. You can specify that part of primary key columns are used 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 data partition. |
IndexSort | The presorting settings of the search index, including Sorters. If IndexSort is not
set, data is sorted by primary key.
Note Search indexes that contain Nested fields do not support IndexSort.
Sorters: required. This parameter specifies the presorting method for the search index.
PrimaryKeySort and FieldSort are supported. For more information, see Sorting and paging.
|
Examples
- Example 1
/// <summary> /// Create a search index that contains the Keyword_type_col, Long_type_col, and Text_type_col attribute columns. Set the type of data in Keyword_type_col to Keyword, in Long_type_col to Long, and in Text_type_col to Text. /// </summary> /// <param name="otsClient"></param> public static void CreateSearchIndex(OTSClient otsClient) { // Set the names of the table and search index. CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName); List<FieldSchema> FieldSchemas = new List<FieldSchema>() { new FieldSchema(Keyword_type_col,FieldType.KEYWORD){ // Set the field name and field type. index =true, // Set index to true. EnableSortAndAgg =true // Set EnableSortAndAgg to true to enable the sorting and aggregation features. }, new FieldSchema(Long_type_col,FieldType.LONG){ index=true,EnableSortAndAgg=true}, new FieldSchema(Text_type_col,FieldType.TEXT){ index=true} }; request.IndexSchame = new IndexSchema() { FieldSchemas = FieldSchemas }; // Call a client to create the search index. CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request); Console.WriteLine("Searchindex is created: " + IndexName); }
- Example 2
Specify IndexSort when you create a search index.
/// <summary> /// Create a search index that contains the Keyword_type_col, Long_type_col, and Text_type_col attribute columns. Set the type of data in Keyword_type_col to Keyword, in Long_type_col to Long, and in Text_type_col to Text. /// </summary> /// <param name="otsClient"></param> public static void CreateSearchIndexWithIndexSort(OTSClient otsClient) { // Set the names of the table and search index. CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName); List<FieldSchema> FieldSchemas = new List<FieldSchema>() { new FieldSchema(Keyword_type_col,FieldType.KEYWORD){ // Set the field name and field type. index =true, // Set index to true. EnableSortAndAgg =true // Set EnableSortAndAgg to true to enable the sorting and aggregation features. }, new FieldSchema(Long_type_col,FieldType.LONG){ index=true,EnableSortAndAgg=true}, new FieldSchema(Text_type_col,FieldType.TEXT){ index=true} }; request.IndexSchame = new IndexSchema() { FieldSchemas = FieldSchemas, // Presort data by the Long_type_col column. You must index the Long_type_col column and enable the sorting and aggregation features. IndexSort = new DataModel.Search.Sort.Sort() { Sorters = new List<DataModel.Search.Sort.ISorter> { new DataModel.Search.Sort.FieldSort(Long_type_col, DataModel.Search.Sort.SortOrder.ASC) } } }; CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request); Console.WriteLine("Searchindex is created: " + IndexName); }