You can call the CreateSearchIndex operation to create a search index for a table.

If a table needs to support complex queries, you must create a search index for the table. When you create a search index, you must specify the table_name, index_name, and index_meta parameters.

  • table_name: the name of the table for which the search index is created.
  • index_name: the name of the search index to create.
  • index_meta: the search index schema, including the index_setting, FieldSchema, and index_sort parameters.
    • FieldSchema: the list of field schemas. Each field schema contains the following parameters:
      • field_name: required. This parameter specifies the name of the field (column) to index. Type: str. The field can be a primary key column or an attribute column.
      • field_type: required. This parameter specifies the type of the field. Type: FieldType. For more information, see the description of field types for a search index.
      • is_array: optional. This parameter specifies whether the column is an array. Type: bool. A value of true indicates that the column is an array. Data written to the column must be a JSON array. Example: ["a","b","c"]. You do not need to explicitly specify this parameter for NESTED columns because they are arrays.
      • index: optional. This parameter specifies whether to index the column. Type: bool.
      • analyzer: optional. This parameter specifies the tokenizer. You can specify this parameter if the column is a TEXT column. Type: AnalyzerType.
      • enable_sort_and_agg: optional. This parameter specifies whether to enable sorting and aggregation for the column. Type: bool.
      • store: optional. This parameter specifies whether to store the values of the field in the search index. Type: bool. A value of true indicates that you can read the values of the field directly from the search index without the need to query the table. This configuration optimizes query performance.
      • sub_field_schemas: optional. This parameter specifies the list of field schemas for subfields. If the column is a NESTED column, you must specify this parameter to configure the index types of subcolumns in the NESTED column.
    • index_setting: optional.

      routing_fields: optional. You can use this advanced feature to customize 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 values is distributed to the same data partition.

    • index_sort: optional. This parameter specifies how data is sorted. By default, the data is sorted in the same way as the primary key of the table. If the search index contains NESTED fields, data is not sorted by default.
      • sorters: required. This parameter specifies the sorting methods. Valid values:
        • PrimaryKeySort: The index is sorted by primary key in ascending or descending order.
        • FieldSort: The index is sorted by a specified column in ascending or descending order.
field_a = FieldSchema('k', FieldType.KEYWORD, index=True, enable_sort_and_agg=True, store=True)
field_b = FieldSchema('t', FieldType.TEXT, index=True, store=True, analyzer=AnalyzerType.SINGLEWORD)
field_c = FieldSchema('g', FieldType.GEOPOINT, index=True, store=True)
field_d = FieldSchema('ka', FieldType.KEYWORD, index=True, is_array=True, store=True)
field_e = FieldSchema('la', FieldType.LONG, index=True, is_array=True, store=True)

field_n = FieldSchema('n', FieldType.NESTED, sub_field_schemas=[
    FieldSchema('nk', FieldType.KEYWORD, index=True, store=True),
    FieldSchema('nl', FieldType.LONG, index=True, store=True),
    FieldSchema('nt', FieldType.TEXT, index=True, store=True),
])

fields = [field_a, field_b, field_c, field_d, field_e, field_n]

index_setting = IndexSetting(routing_fields=['PK1'])
index_sort = None # can not set index sort if there is any NESTED field.
#index_sort = Sort(sorters=[PrimaryKeySort(SortOrder.ASC)])
index_meta = SearchIndexMeta(fields, index_setting=index_setting, index_sort=index_sort)
client.create_search_index(table_name, index_name, index_meta)