After you create a search index for a table, you can query fields only in the search index when you use the search index. You can create multiple search indexes for a table.
Background information
The actual query requirements determine the number of search indexes created for a table.
Assume that you have a table that contains the following fields: id, name, age, city, and sex. To query data by name, age, or city, you can use one of the following methods to create a search index:
- Method 1: Create a search index for each index field
If you use this method, you must create the following search indexes: name_index, age_index, and city_index.
- To query students by city, use city_index. To query students by age, use age_index.
- However, this method does not work if you want to query students who are younger than 12 years old and live in Chengdu.
The implementation of the method is similar to that of global secondary index. However, this method is not cost-effective. We recommend that you do not use this method to create a search index.
- Method 2: Create one search index for multiple index fields
If you use this method, the search index named student_index contains the following fields: name, age, and city.
- To query students by city, use the city field in student_index. To query students by age, use the age field in student_index.
- To query students who are younger than 12 years old and live in Chengdu, use the age and city fields in student_index.
This method provides more functions at a lower cost. We recommend that you use this method to create search indexes.
Limits
- Timeliness of index creation
It takes a few seconds to use a search index after the search index is created. In the creation process, you can write data to the table. However, metadata queries of the search index and data queries by using the search index are affected.
- Limits on the number of resources
For more information, see Limits.
Operations
You can call the CreateSearchIndex operation to create a search index.
Use Tablestore SDKs
- Tablestore SDK for Java: Create search indexes
- Tablestore SDK for Go: Create search indexes
- Tablestore SDK for Python: Create search indexes
- Tablestore SDK for Node.js: Create search indexes
- Tablestore SDK for .NET: Create search indexes
- Tablestore SDK for PHP SDK: Create search indexes
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. By default, data is
sorted by primary key if indexSort is not set.
Note Search indexes that contain NEST 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 Sort.
|
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); }