You can call the CreateSearchIndex operation to create one or more search indexes for a data table.

Prerequisites

  • OTSClient is initialized. For more information, see Initialization.
  • A data table whose time_to_live is set to -1 and max_versions is set to 1 is created.

Parameters

When you create a search index, you must specify table_name, index_name, and schema. You must specify field_schemas, index_setting, and index_sort in schema. The following table describes the parameters.

Parameter Description
table_name The name of the table.
index_name The name of the search index.
field_schemas The list of field schemas. You can configure the following parameters for each field schema:
  • field_name: required. This parameter specifies the name of the field in the search index. The value is a column name. Type: String.

    A field in a search index can be a primary key column or an attribute column.

  • field_type: required. This parameter specifies the type of a field. Use FieldType.XXX to set the type. For more information, see Data types of column values.
  • is_array: optional. This parameter specifies whether the value is an array. Type: Boolean.

    If you set this parameter to true, the column stores data as an array. Data written to the column must be a JSON array. Example: ["a","b","c"].

    Nested values are an array. If you set field_type to Nested, skip this parameter.

  • index: optional. This parameter specifies whether to create an index for the column. Type: Boolean.

    By default, this parameter is set to true, and Tablestore creates an inverted index or spatial index for the column. If this parameter is set to false, Tablestore does not create indexes for the column.

  • analyzer: optional. This parameter specifies the type of analyzer to use. If field_type is set to Text, you can set this parameter. If you do not specify this parameter, single-word tokenization is used. For more information about tokenization, see Tokenization.
  • enable_sort_and_agg: optional. This parameter specifies whether to enable the sorting and aggregation features. Type: Boolean.

    Before you enable the sorting feature, you need to set enable_sort_and_agg to true for a field. For more information about sorting, see Sorting and pagination.

  • store: optional. This parameter specifies whether to store the value of the field in the search index. Type: Boolean.

    If you set the value to true, you can read the value of the field directly from the search index without querying the entire table. This improves 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 The settings of the search index, including routing_fields.

routing_fields: optional. This parameter specifies custom 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 partition.

index_sort The presorting settings of the search index, including sorters. If indexSort is left empty, data is sorted by primary key.
Note You can skip the presorting settings for search indexes that contain the Nested field type.
sorters: required. This parameter specifies the presorting method for the search index. PrimaryKeySort and FieldSort are supported. For more information about sorting, see Sorting and pagination.
  • PrimaryKeySort: Data is sorted by primary key. You can configure the following parameter for PrimaryKeySort:

    sort_order: the sort order. Data can be sorted in ascending or descending order. Default value: SortOrder.ASC.

  • FieldSort: Data is sorted by field value. You can configure the following parameters for FieldSort:

    You can presort field values only when a search index is created and the sorting and aggregation features are enabled for fields in the search index.

    • field_name: the name of the field to sort.
    • sort_order: the sort order. Data can be sorted in ascending or descending order. Default value: SortOrder.ASC.
    • sort_mode: the sorting method used when the field has multiple values.

Examples

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)