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 timeToLive is set to -1 and maxVersions is set to 1 is created.

Parameters

When you create a search index, you must specify tableName, indexName, and indexSchema. You must specify fieldSchemas, indexSetting, and indexSort in indexSchema. The following table describes the parameters.
Parameter Description
tableName The name of the table.
indexName The name of the search index.
fieldSchemas The list of field schemas. Each field schema contains the following parameters:
  • fieldName: 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.

  • fieldType: required. This parameter specifies the type of the field. Use FieldType.XXX to set the type. For more information, see Data types of column values.
  • 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 considered as an array. If you set fieldType to Nested, skip the array 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 fieldType 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.
  • enableSortAndAgg: optional. This parameter specifies whether to enable the sorting and aggregation features. Type: Boolean.

    Beofor you enable the sorting feature, you need to set enableSortAndAgg 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 store parameter to true, you can read the value of the field directly from the search index without querying the entire table. This improves query performance.

  • isVirtualField: optional. This parameter specifies whether the field is a virtual column. Type: Boolean. This parameter is required only when you use virtual columns. For more information about virtual columns, see Virtual columns.
  • sourceFieldName: optional. This parameter specifies the name of the source field to which the virtual column is mapped in the table. Type: String. This parameter is required when isVirtualField is set to true.
indexSetting The settings of the search index, including routingFields.

routingFields: 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.

indexSort 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:

    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.

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

Examples

  • Create a search index
    Create a search index that consists of the Col_Keyword and Col_Long columns. Set the type of data in Col_Keyword to String and Col_Long to Long.
    
    private static void createSearchIndex(SyncClient client) {
        CreateSearchIndexRequest request = new CreateSearchIndexRequest();
        request.setTableName(TABLE_NAME); // Set the name of the data table. 
        request.setIndexName(INDEX_NAME); // Set the name of the search index. 
        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 the index. 
                        .setEnableSortAndAgg(true), // Set the parameter to true to enable sorting and aggregation. 
                        .setStore(true), // Set Store to true to store the value of the column in the search index. 
                new FieldSchema("Col_Long", FieldType.LONG)
                        .setIndex(true)
                        .setEnableSortAndAgg(true)
                        .setStore(true)));
        request.setIndexSchema(indexSchema);
        client.createSearchIndex(request); // Call client to create the search index. 
    }
  • Specify IndexSort when you create a search index
    private static void createSearchIndexWithIndexSort(SyncClient client) {
        CreateSearchIndexRequest request = new CreateSearchIndexRequest();
        request.setTableName(TABLE_NAME);
        request.setIndexName(INDEX_NAME);
        IndexSchema indexSchema = new IndexSchema();
        indexSchema.setFieldSchemas(Arrays.asList(
                new FieldSchema("Col_Keyword", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true).setStore(true),
                new FieldSchema("Col_Long", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true).setStore(true),
                new FieldSchema("Col_Text", FieldType.TEXT).setIndex(true).setStore(true),
                new FieldSchema("Timestamp", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true).setStore(true)));
        // Presort data by the Timestamp column. You must create a search index and enable sorting and aggregation for the Timestamp column. 
        indexSchema.setIndexSort(new Sort(
                Arrays.<Sort.Sorter>asList(new FieldSort("Timestamp", SortOrder.ASC))));
        request.setIndexSchema(indexSchema);
        client.createSearchIndex(request);
    }
  • Specify a virtual column when you create a search index
    Create a search index. The search index contains the Col_Keyword and Col_Long columns. Create virtual columns Col_Keyword_Virtual_Long and Col_Long_Virtual_Keyword. Map Col_Keyword_Virtual_Long to the Col_Keyword column in the data table, and Col_Long_Virtual_Keyword to the Col_Long column in the data table.
    private static void createSearchIndex(SyncClient client) {
        CreateSearchIndexRequest request = new CreateSearchIndexRequest();
        request.setTableName(TABLE_NAME); // Set the name of the data table. 
        request.setIndexName(INDEX_NAME); // Set the name of the search index. 
        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 the index. 
                .setEnableSortAndAgg(true), // Set the parameter to true to enable sorting and aggregation. 
                .setStore(true),
            new FieldSchema("Col_Keyword_Virtual_Long", FieldType.LONG) // Set the name and the type of the field. 
                .setIndex(true)
                .setEnableSortAndAgg(true)
                .setStore(true)
                .setVirtualField(true) // Specify whether the field is a virtual column. 
                .setSourceFieldName("Col_Keyword"), // Map the virtual column to the field in the data table. 
            new FieldSchema("Col_Long", FieldType.LONG)
                .setIndex(true)
                .setEnableSortAndAgg(true)
                .setStore(true),
            new FieldSchema("Col_Long_Virtual_Keyword", FieldType.KEYWORD)
                .setIndex(true)
                .setEnableSortAndAgg(true)
                .setStore(true)
                .setVirtualField(true)
                .setSourceFieldName("Col_Long")));
        request.setIndexSchema(indexSchema);
        client.createSearchIndex(request); // Call client to create the search index. 
    }