To enable search index on a table, you must create a search index for the table.

Note
  • You can create multiple search indexes for a table.
  • You can use search index to query just the fields (including primary key columns and attribute columns) included within the search index.

Key points

How many indexes can be created for 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 search indexes for each index field

    In this case, you must 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, 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 this method is similar to that of secondary indexes. In this case, one index field to one search index increases costs but brings no benefits. Therefore, we recommend that you do not use this method to create search indexes.

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

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

    You can use the age and city index fields in 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 to create search indexes.

Description

You can call the CreateSearchIndex operation to create search indexes. The parameters are described as follows.
  • 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: specifies the schema of the search index.
    • IndexSetting
      RoutingFields: specifies the routing fields. You can specify some primary key columns 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 value is distributed to the same data partition.
      Note You can only specify Tablestore primary key columns as routing fields.
    • 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.
      • Analyzer: optional. This parameter specifies the tokenization method. Multiple tokenization methods are available. For more information, see Tokenization.
      • 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.

Limits

  • Timeliness of index creation

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

  • Quantity

    For more information, see Limits.

Example

/**
 *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.
 }