You can call the CreateSearchIndex operation to create one or more search indexes for a data table. When you create a search index, you can add the fields that you want to query to the search index and configure advanced settings for the search index. For example, you can configure the routing key and presorting settings.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created. The TimeToLive parameter is set to -1 and the MaxVersions parameter is set to 1 for the data table.
Usage notes
The data types of the fields in a search index must match the data types of the fields in the data table for which the search index is created. For more information, see Data type mappings.
Parameters
When you create a search index, you must configure the TableName, IndexName, and IndexSchema parameters. You must also configure the FieldSchemas, IndexSetting, and IndexSort parameters in the IndexSchema parameter. The following table describes the 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 settings of the search index, including RoutingFields. RoutingFields: optional. The 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 records whose routing field values are the same are distributed to the same partition. |
IndexSort | The presorting settings for the search index, including Sorters. By default, if you do not specify this parameter, the field values are sorted based on the primary key. Note You can skip the presorting settings for search indexes that contain fields of the Nested type. Sorters: required. The presorting method for the search index. PrimaryKeySort and FieldSort are supported. For more information, see Sorting and paging.
|
TimeToLive | Optional. The time-to-live (TTL) of the search index. Default value: -1. The TTL of the search index is the retention period of data in the search index. If the retention period of data exceeds the TTL, the data expires. Tablestore automatically deletes expired data. The value of this parameter must be greater than or equal to 86400. A value of 86400 specifies one day. You can also set this parameter to -1, which specifies that data never expires. |
Examples
Create a search index by using the default configurations
The following sample code shows how to create a search index by using the default configurations. In this example, the search index consists of the following columns: the Keyword_type_col column of the Keyword type, the Long_type_col column of the Long type, and the Text_type_col column of the Text type. The sorting and aggregation are enabled.
/// <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 the Keyword_type_col column to Keyword, in the Long_type_col column to Long, and in the Text_type_col column 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);
}
Create a search index with the IndexSort parameter specified
The following sample code shows how to create a search index with the IndexSort parameter specified. In this example, the search index consists of the following columns: the Keyword_type_col column of the Keyword type, the Long_type_col column of the Long type, and the Text_type_col column of the Text type. The data in the search index is presorted based on the Long_type_col column.
/// <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 the Keyword_type_col column to Keyword, in the Long_type_col column to Long, and in the Text_type_col column to Text.
/// </summary>
/// <param name="otsClient"></param>
public static void CreateSearchIndexWithIndexSort(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,
// Presort data based on 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);
}
Create a search index that contains a column of the Date type and a virtual column.
The following sample code shows how to create a search index that contains a column of the Date type and a virtual column. In this example, the search index consists of the following columns: the pk0 column of the Keyword type, the pk1 column of the Long type, the date_col column of the Date type, the geo_col column of the Geo-point type, and the col0_v1 column of the Text type. The source column of the col0_v1 virtual column is the col0 column. The returned results are sorted in ascending order based on the pk1 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);
}
FAQ
References
After you create a search index, you can use the query methods provided by the search index to query data from multiple dimensions based on your business requirements. The following query methods are supported: term query, terms query, match all query, match query, phrase query, prefix query, range query, wildcard query, geo query, Boolean query, nested query, and exists query.
If you call the Search operation to query data, you can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.
If you call the Search operation to query data, you can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).
If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.
If you want to quickly obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Perform a parallel scan.
You can dynamically modify the schema of a search index to add, update, or remove index columns in the search index. For more information, see Dynamically modify the schema of a search index.
You can call the ListSearchIndex operation to query all search indexes that are created for a data table. For more information, see List search indexes.
You can call the DescribeSearchIndex operation to query the description of a search index. For example, you can query the field information and search index configurations. For more information, see Query the description of a search index.
You can delete a search index that you no longer need. For more information, see Delete a search index.