You can call this operation to create a search index. To use the search index feature for a table, you must create a search index in the table. One table can contain multiple search indexes.

You can call the Search operation to query fields (including primary key columns and attribute columns) included in the search index.

Description

Parameters:
  • TableName: specifies the name of the table for which you want to create a search index.
  • IndexName: specifies the name of the search index.
  • IndexSchema: defines the schema of the search index.
    • IndexSetting
      • RoutingFields: specifies the routing fields. You can specify some primary key columns as routing fields. Table Store 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.
    • FieldSchemas
      • FieldName: required. This parameter specifies the name of the field that is a column name in the table. The name is of the string type.
      • FieldType: required. This parameter specifies the type of the field. For more information, see the "Fields" section in Overview.
      • Index: optional. This parameter specifies whether to create an index for the field. The index is of the Boolean type. Default value: true.
      • IndexOptions: optional. This parameter specifies whether to store terms such as position and offset in an inverted list. Use the default value in general conditions.
      • EnableSortAndAgg: optional. This parameter specifies whether to enable sorting and aggregation. This parameter is of the Boolean type. Default value: true.
      • Store: optional. This parameter specifies whether to store original values in the index to accelerate queries. This parameter is of the Boolean type. Default value: true.

FAQ

How many indexes can be created in a table?

Assume that you have a table with five fields: ID, name, age, city, and sex, and you need to query by name, age, or city. There are two methods to create search indexes:

  • Method 1: Create a search index for an index field

    In this case, you need to create three search indexes: name_index, age_index, and city_index. You can use city_index to query data by city, and age_index to query data by age.

    However, you cannot use this method to query students who are younger than 12 years old and live in Chengdu.

    The implementation of this method is similar to that of secondary indexes. In this case, one index field for one search index brings no benefits to search indexing but increases costs. Therefore, we recommend that you do not use this method to create a search index.

  • Method 2: Create a search index for multiple index fields

    In this case, you only need to create a search index named student_index. The fields include name, age, and city. You can use the city index field in the student_index to query data by city. You can use the age index field in the student_index to query data by age.

    You can use the age and city index fields in the student_index to query students who are younger than 12 years old and live in Chengdu.

    This method provides more functions at low cost. We recommend that you use this method.

Limits

1. Timeliness of index creation

It takes a few minutes to create a search index. During the creation process, you can write data into the table.

2. Quantity

For more information, see Limits.

Examples

/**
 *Create a search index that contains the Col_Keyword and Col_Long columns. Set the type of data in Col_Keyword to KEYWORD. Set the type of data in Col_Long to LONG.
 */
private static void createSearchIndex(SyncClient client) {
    CreateSearchIndexRequest request = new CreateSearchIndexRequest();
    request.setTableName(TABLE_NAME); // Set the table name.
    request.setIndexName(INDEX_NAME); // Set the index name.
    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 indexing.
                    .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); // Use the client to create a search index.
}