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 creating 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 be created.
  • index_meta: the index schema, including the index_setting, field_schemas, and index_sort parameters.
    • field_schemas: the list of FieldSchemas. Each FieldSchema contains the following parameters:
      • field_name (required, String): the name of the field to be indexed, that is, the column name. The field can be a primary key column or an attribute column.
      • field_type (required, FieldType): the type of the field. For more information, see the description of field types for a search index.
      • is_array (optional, Boolean): specifies whether the column is an array. A value of true indicates that the column is an array. Data written to the column must be a JSON array, such as ["a","b","c"]. You do not need to explicitly specify this parameter for nested columns because they are arrays.
      • index (optional, Boolean): specifies whether to index the column.
      • analyzer (optional, AnalyzerType): the tokenizer. You can specify this parameter if the column is a text column.
      • enable_sort_and_agg (optional, Boolean): specifies whether to enable sorting and statistics collection for the column.
      • store (optional, Boolean): specifies whether to store the values of the field in the search index. 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 optimizes query performance.
      • sub_field_schemas (optional, FieldSchema list): the list of FieldSchemas for sub-fields. If the column is a nested column, you must specify this parameter to configure the index types of sub-columns in the nested column.
    • index_setting (optional):

      routing_fields (optional, advanced feature): the custom 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 partition.

    • index_sort (optional): specifies how the index is sorted. The index is sorted in the same way as the primary key of the table by default. (If the index contains nested fields, it is not sorted by default).
      • sorters (required, Sort array): the sorting methods. Valid values:
        • PrimaryKeySort: The index is sorted by the 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 # The index cannot be sorted 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)