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
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(TABLE_NAME); // Set the name of the table. request.setIndexName(INDEX_NAME); // Set the name of the search index. IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("Col_Keyword", FieldType.KEYWORD) // Set the field name and field type. .setIndex(true) // Set the parameter to true to enable the index. .setEnableSortAndAgg(true), // Set the parameter to true to enable sorting and aggregation. new FieldSchema("Col_Long", FieldType.LONG) .setIndex(true) .setEnableSortAndAgg(true))); request.setIndexSchema(indexSchema); client.createSearchIndex(request); // Call client to create the search index. }
- Example 2
Specify IndexSort when you create a search index.
private static void createSearchIndexWithIndexSort(SyncClient client) { CreateSearchIndexRequest request = new CreateSearchIndexRequest(); request.setTableName(TABLE_NAME); request.setIndexName(INDEX_NAME); IndexSchema indexSchema = new IndexSchema(); indexSchema.setFieldSchemas(Arrays.asList( new FieldSchema("Col_Keyword", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true), new FieldSchema("Col_Long", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true), new FieldSchema("Col_Text", FieldType.TEXT).setIndex(true), new FieldSchema("Timestamp", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true))); // Presort data by the Timestamp column. You must create a search index and enable sorting and aggregation for the Timestamp column. indexSchema.setIndexSort(new Sort( Arrays. <Sort.Sorter>asList(new FieldSort("Timestamp", SortOrder.ASC)))); request.setIndexSchema(indexSchema); client.createSearchIndex(request); }