You can call the CreateSearchIndex operation to create one or more search indexes for a data table.
Prerequisites
- An OTSClient instance is initialized. For more information, see Initialization.
- A data table is created. The TimeToLive parameter is set to -1 and the MaxVersions parameter is set to 1 for the data table.
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: This parameter is optional and specifies custom routing fields. You can specify some primary key columns as routing fields. Tablestore distributes data that is written to a search index across different partitions based on the specified routing fields. The data whose routing field values are the same is distributed to the same partition. |
IndexSort | The presorting settings for the search index, including Sorters. If no value is specified for the IndexSort parameter, field values are sorted by primary key by default.
Note You can skip the presorting settings for the search indexes that contain fields of the Nested type.
Sorters: This parameter is required and specifies the presorting method for the search index. PrimaryKeySort and FieldSort are supported. For more information about sorting, see
Sorting and paging.
|
TimeToLive | This parameter is optional and specifies the retention period of data in the search index. Unit: seconds. Default value: -1. If the retention period exceeds the TTL value, Tablestore automatically deletes expired data. The minimum timeToLive value is 86400, which is equal to one day. A value of -1 specifies that data never expires. |
Examples
- Example 1
The following sample code shows how to 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 CreateSearchIndex(OTSClient otsClient) { // Specify the names of the table and the search index. CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName); List<FieldSchema> FieldSchemas = new List<FieldSchema>() { new FieldSchema(Keyword_type_col,FieldType.KEYWORD){ // Specify the name and type of the field. index =true, // Enable the indexing feature. EnableSortAndAgg = true // Enable sorting and aggregation. }, 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
The following sample code shows how to create a search index with IndexSort specified:
/// <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) { // Specify 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){ // Specify the name and type of the field. index =true, // Enable the indexing feature. EnableSortAndAgg = true // Enable sorting and aggregation. }, 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 sorting and aggregation for the column. 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); }
- Example 3
The following sample code shows how to create a search index that contains a column of the Date type and a virtual column:
/// <summary> /// Create a search index that contains a column of the Date type and a virtual column. /// </summary> /// <param name="otsClient"></param> public static void CreateSearchIndex(OTSClient otsClient) { List<FieldSchema> fieldSchemas = new List<FieldSchema> { new FieldSchema("pk0", FieldType.KEYWORD) { index = true, EnableSortAndAgg = true }, new FieldSchema("pk1", FieldType.LONG) { index = true, EnableSortAndAgg = true }, new FieldSchema("date_col", FieldType.DATE) { index = true, DateFormats = new List<string>(){ "yyyy-MM-dd'T'HH:mm:ss.SSSSSS", "yyyy-MM-dd'T'HH:mm:ss.SSS" } }, new FieldSchema("geo_col", FieldType.GEO_POINT) { index = true, EnableSortAndAgg = true }, new FieldSchema("col0_v1", FieldType.TEXT) { index = true, Analyzer = Analyzer.Split, AnalyzerParameter = new SingleWordAnalyzerParameter(true, true), IsVirtualField = true, SourceFieldNames = new List<string> { "col0" } }, }; CreateSearchIndexRequest request = new CreateSearchIndexRequest(TableName, IndexName); request.IndexSchame = new IndexSchema() { FieldSchemas = fieldSchemas, IndexSort = new Sort(new List<ISorter> { new FieldSort("pk1", SortOrder.ASC) }) }; request.TimeToLive = -1; otsClient.CreateSearchIndex(request); }