You can call the CreateSearchIndex operation to create one or more search indexes for a data table.
Prerequisites
- A Tablestore client is initialized. For more information, see Initialization.
- A data table is created for which the value of the timeToLive parameter is set to -1 and the value of the maxVersions parameter is set to 1.
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: 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 of 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 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 pagination.
|
timeToLive | This parameter is optional and specifies the retention period of data in the search
index. Unit: seconds. Default value: -1.
When the retention period exceeds the timeToLive 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
- Create a search index
The following sample code shows how to create a search index that consists of the Col_Keyword and Col_Long columns. Set the type of data in Col_Keyword to String and Col_Long to Long.
private static void createSearchIndex(SyncClient client) { CreateSearchIndexRequest request = new CreateSearchIndexRequest(); request.setTableName(tableName); // Specify the name of the data table. request.setIndexName(indexName); // Specify the name of the search index. IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("Col_Keyword", FieldType.KEYWORD) // Specify the name and type of the field. .setIndex(true) // Enable indexing. .setEnableSortAndAgg(true) // Enable sorting and aggregation. .setStore(true), // Specify that the value of the field is stored in the search index. new FieldSchema("Col_Long", FieldType.LONG) .setIndex(true) .setEnableSortAndAgg(true) .setStore(true))); request.setIndexSchema(indexSchema); client.createSearchIndex(request); // Call a client to create the search index. }
- Create a search index with indexSort specified
private static void createSearchIndexWithIndexSort(SyncClient client) { CreateSearchIndexRequest request = new CreateSearchIndexRequest(); request.setTableName(tableName); request.setIndexName(indexName); IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("Col_Keyword", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true).setStore(true), new FieldSchema("Col_Long", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true).setStore(true), new FieldSchema("Col_Text", FieldType.TEXT).setIndex(true).setStore(true), new FieldSchema("Timestamp", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true).setStore(true))); // Presort data by the Timestamp column. You must enable indexing and set enableSortAndAgg to true for the Timestamp column. indexSchema.setIndexSort(new Sort( Arrays.<Sort.Sorter>asList(new FieldSort("Timestamp", SortOrder.ASC)))); request.setIndexSchema(indexSchema); client.createSearchIndex(request); }
- Create a search index with the TTL specified
Notice Make sure that updates to the data table are prohibited.
// Use Tablestore SDK for Java V5.12.0 or later to create a search index. public void createIndexWithTTL() { int days = 7; CreateSearchIndexRequest createRequest = new CreateSearchIndexRequest(); createRequest.setTableName(tableName); createRequest.setIndexName(indexName); createRequest.setIndexSchema(indexSchema); // Specify the TTL for the search index. createRequest.setTimeToLiveInDays(days); client.createSearchIndex(createRequest); }
- Create a search index with virtual columns specified
The following sample code shows how to create a search index that contains columns Col_Keyword and Col_Long. Each of the columns has a virtual column. The virtual column of the Col_Keyword column is Col_Keyword_Virtual_Long and that of the Col_Long column is Col_Long_Virtual_Keyword. The Col_Keyword_Virtual_Long column is mapped to the Col_Keyword column in the data table, and the Col_Long_Virtual_Keyword column is mapped to the Col_Long column in the data table.
private static void createSearchIndex(SyncClient client) { CreateSearchIndexRequest request = new CreateSearchIndexRequest(); request.setTableName(tableName); // Specify the name of the data table. request.setIndexName(indexName); // Specify the name of the search index. IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("Col_Keyword", FieldType.KEYWORD) // Specify the name and type of the field. .setIndex(true) // Enable indexing. .setEnableSortAndAgg(true) // Enable sorting and aggregation. .setStore(true), new FieldSchema("Col_Keyword_Virtual_Long", FieldType.LONG) // Specify the name and type of the field. .setIndex(true) .setEnableSortAndAgg(true) .setStore(true) .setVirtualField(true) // Specify whether the field is a virtual column. .setSourceFieldName("Col_Keyword"), // Specify name of the source field to which the virtual column is mapped in the data table. new FieldSchema("Col_Long", FieldType.LONG) .setIndex(true) .setEnableSortAndAgg(true) .setStore(true), new FieldSchema("Col_Long_Virtual_Keyword", FieldType.KEYWORD) .setIndex(true) .setEnableSortAndAgg(true) .setStore(true) .setVirtualField(true) .setSourceFieldName("Col_Long"))); request.setIndexSchema(indexSchema); client.createSearchIndex(request); // Call a client to create the search index. }