创建多元索引

更新时间:2025-04-29 07:10:04

使用CreateSearchIndex方法在数据表上创建一个多元索引。一个数据表支持创建多个多元索引。创建多元索引时,您需要将要查询的字段添加到多元索引中,您还可以配置多元索引路由键、预排序等高级选项。

前提条件

注意事项

  • 创建多元索引时,多元索引中字段的数据类型必须与数据表中字段的数据类型相匹配。

  • 如果要修改多元索引为指定数据生命周期(即取值不为-1),则您必须禁用数据表的UpdateRow更新写入功能。同时多元索引的TTL值必须小于或等于数据表的TTL值。更多信息,请参见生命周期管理

参数

创建多元索引时,需要指定数据表名称(tableName)、多元索引名称(indexName)和索引的结构信息(schema),其中schema包含fieldSchemas(Index的所有字段的设置)、indexSetting(索引设置)和indexSort(索引预排序设置)。详细参数说明请参见下表。

参数

说明

参数

说明

tableName

数据表名称。

indexName

多元索引名称。

fieldSchemas

fieldSchemas的列表,每个fieldSchema包含如下内容:

  • fieldName(必选):创建多元索引的字段名,即列名,类型为String。

    多元索引中的字段可以是主键列或者属性列。

  • fieldType(必选):字段的数据类型,类型为TableStore.FieldType.XXX。

  • index(可选):是否开启索引,类型为Boolean。

    默认为true,表示对该列构建倒排索引或者空间索引;如果设置为false,则不会对该列构建索引。

  • analyzer(可选):分词器类型。当字段类型为Text时,可以设置此参数;如果不设置,则默认分词器类型为单字分词。

  • analyzerParameter(可选):分词参数配置,请根据不同分词器类型设置相应参数。当为字段配置了analyzer时,必须设置此参数。

  • enableSortAndAgg(可选):是否开启排序与统计聚合功能,类型为Boolean。

    只有enableSortAndAgg设置为true的字段才能进行排序

    重要

    Nested类型的字段不支持开启排序与统计聚合功能,但是Nested类型内部的子列支持开启排序与统计聚合功能。

  • store(可选):是否在多元索引中附加存储该字段的值,类型为Boolean。

    开启后,可以直接从多元索引中读取该字段的值,而不必反查数据表,可用于查询性能优化。

  • isAnArray(可选):是否为数组,类型为Boolean。

    如果设置为true,则表示该列是一个数组,在写入时,必须按照JSON数组格式写入,例如["a","b","c"]。

    由于Nested类型是一个数组,当fieldTypeNested类型时,无需设置此参数。

  • fieldSchemas(可选):当字段类型为Nested类型时,需要通过此参数设置嵌套文档中子列的索引类型,类型为fieldSchema的列表。

  • isVirtualField(可选):该字段是否为虚拟列,类型为Boolean类型,默认值为false。如果要使用虚拟列,请设置此参数为true。

  • sourceFieldName(可选):数据表中的字段名称,类型为String。当设置isVirtualFieldtrue时,必须设置此参数。

  • dateFormats(可选):日期的格式,类型为String。当字段类型为Date时,必须设置此参数。更多信息,请参见日期时间类型

  • enableHighlighting(可选):是否开启摘要与高亮功能,类型为Boolean。默认值为false,表示不开启摘要与高亮。如果要使用摘要与高亮,请设置此参数为true。仅Text类型字段支持摘要与高亮功能。

    重要

    表格存储Node.js SDK5.5.0版本开始支持此功能。

  • vectorOptions(可选):向量字段类型的属性参数。当字段类型为 Vector 时,必须设置此参数。包括如下内容:

    • dataType:向量数据类型。当前仅支持 float32。如果有其他类型需求,请提交工单联系我们。

    • dimension:向量维度。向量类型字段支持的最大维度 (Dimension)为2048。

    • metricType:向量之间距离度量的算法,支持欧氏距离(euclidean)、余弦相似度(cosine)、点积(dot_product)。

      • 欧氏距离(euclidean):多维空间中两个向量之间的直线距离。出于性能考虑,表格存储中的欧氏距离算法未进行最后的平方根计算。欧氏距离的评分越大表示两个向量的相似度越大。

      • 余弦相似度(cosine):向量空间中两个向量间夹角的余弦值。余弦相似度的评分越高表示两个向量的相似度越大。常用于文本数据的相似度计算。

      • 点积(dot_product):维度相同的两个向量的对应坐标相乘,然后将结果相加。点积的评分越高标识两个向量的相似度越大。

      选用距离度量算法的说明请参见距离度量算法说明

indexSetting

索引设置,包含routingFields设置。

routingFields(可选):自定义路由字段。可以选择部分主键列作为路由字段,一般情况下只需要设置一个。如果设置多个路由键,系统会将多个路由键的值拼接成一个值。

在进行索引数据写入时,系统会根据路由字段的值计算索引数据的分布位置,路由字段的值相同的记录会被索引到相同的数据分区中。

indexSort

索引预排序设置,包含sorters设置。如果不设置,则默认按照主键排序。

说明

含有Nested类型的索引不支持indexSort,没有预排序。

sorters(必选):索引的预排序方式,支持按照主键排序和字段值排序。关于排序的更多信息,请参见排序和翻页

  • PrimaryKeySort表示按照主键排序,包含如下设置:

    order:排序的顺序,可按升序或者降序排序,默认为升序(TableStore.SortOrder.SORT_ORDER_ASC)。

  • FieldSort表示按照字段值排序,包含如下设置:

    只有建立索引且开启排序与统计聚合功能的字段才能进行预排序。

    • fieldName:排序的字段名。

    • order:排序的顺序,可按照升序或者降序排序,默认为升序(TableStore.SortOrder.SORT_ORDER_ASC)。

    • mode:当字段存在多个值时的排序方式。

timeToLive

可选参数。数据生命周期(TTL),即数据的保存时间,单位为秒。

默认值为 -1,表示数据永不过期。数据生命周期的取值最低为 86400 秒(一天),也可设置为 -1(永不过期)。

当数据的保存时间超过设置的数据生命周期时,系统会自动清理超过数据生命周期的数据。

示例

创建多元索引时设置分词
创建多元索引时开启摘要与高亮

以下示例用于创建一个多元索引。该多元索引包括pic_id(Keyword类型)、count(Long类型)、time_stamp(Long类型)、pic_description(Text类型)、col_vector(Vector类型)、pos(Geo-point类型)、pic_tag(Nested类型)、date(Date类型)、analyzer_single_word(Text类型)、analyzer_split(Text类型)、analyzer_fuzzy(Text类型)列。其中pic_tag包括sub_tag_name(Keyword类型)和tag_name(Keyword类型)两列,analyzer_single_word列使用的分词类型为单字分词,analyzer_split列使用的分词类型为分隔符分词,analyzer_fuzzy列使用的分词类型为模糊分词。

client.createSearchIndex({
    tableName: "<TABLE_NAME>", //设置数据表名称。
    indexName: "<INDEX_NAME>", //设置多元索引名称。
    schema: {
        fieldSchemas: [
            {
                fieldName: "pic_id",
                fieldType: TableStore.FieldType.KEYWORD, // 设置字段名和字段类型。
                index: true, // 设置开启索引。
                enableSortAndAgg: true, // 设置开启排序和统计功能。
                store: false,
                isAnArray: false
            },
            {
                fieldName: "count",
                fieldType: TableStore.FieldType.LONG,
                index: true,
                enableSortAndAgg: true,
                store: true,
                isAnArray: false
            },
            {
                fieldName: "time_stamp",
                fieldType: TableStore.FieldType.LONG,
                index: true,
                enableSortAndAgg: false,
                store: true,
                isAnArray: false,
            },
            {
                fieldName: "pic_description",
                fieldType: TableStore.FieldType.TEXT,
                index: true,
                enableSortAndAgg: false,
                store: true,
                isAnArray: false,
            },
            {
                fieldName: "col_vector",
                fieldType: TableStore.FieldType.VECTOR,
                index: true,
                isAnArray: false,
                vectorOptions: {
                    dataType: TableStore.VectorDataType.VD_FLOAT_32,
                    dimension: 4,
                    metricType: TableStore.VectorMetricType.VM_COSINE,
                }
            },
            {
                fieldName: "pos",
                fieldType: TableStore.FieldType.GEO_POINT,
                index: true,
                enableSortAndAgg: true,
                store: true,
                isAnArray: false,
            },
            {
                fieldName: "pic_tag",
                fieldType: TableStore.FieldType.NESTED,
                index: false,
                enableSortAndAgg: false,
                store: false,
                fieldSchemas: [
                    {
                        fieldName: "sub_tag_name",
                        fieldType: TableStore.FieldType.KEYWORD,
                        index: true,
                        enableSortAndAgg: true,
                        store: false,
                    },
                    {
                        fieldName: "tag_name",
                        fieldType: TableStore.FieldType.KEYWORD,
                        index: true,
                        enableSortAndAgg: true,
                        store: false,
                    }
                ]
            },
            {
                fieldName: "date",
                fieldType: TableStore.FieldType.DATE,
                index: true,
                enableSortAndAgg: true,
                store: true,
                isAnArray: false,
                dateFormats: ["yyyy-MM-dd'T'HH:mm:ss.SSSSSS"],
            },
            {
                fieldName: "analyzer_single_word",
                fieldType: TableStore.FieldType.TEXT,
                analyzer: "single_word",
                index: true,
                enableSortAndAgg: false,
                store: true,
                isAnArray: false,
                analyzerParameter: {
                    caseSensitive: true,
                    delimitWord: false,
                }
            },
            {
                fieldName: "analyzer_split",
                fieldType: TableStore.FieldType.TEXT,
                analyzer: "split",
                index: true,
                enableSortAndAgg: false,
                store: true,
                isAnArray: false,
                analyzerParameter: {
                    delimiter: ",",
                }
            },
            {
                fieldName: "analyzer_fuzzy",
                fieldType: TableStore.FieldType.TEXT,
                analyzer: "fuzzy",
                index: true,
                enableSortAndAgg: false,
                store: true,
                isAnArray: false,
                analyzerParameter: {
                    minChars: 1,
                    maxChars: 5,
                }
            },
        ],
        indexSetting: { //索引的配置选项。
            "routingFields": ["count", "pic_id"], //只支持将主键列设置为routingFields。
            "routingPartitionSize": null
        },
        //indexSort: {//含有Nested类型的索引不支持indexSort,没有预排序。
            //sorters: [
                // { //不设置indexSort时,默认为PrimaryKeySort(升序)排序。
                //     primaryKeySort: {
                //         order: TableStore.SortOrder.SORT_ORDER_ASC
                //     }
                // },
                //{
                //   fieldSort: {
                //        fieldName: "Col_Keyword",
                //        order: TableStore.SortOrder.SORT_ORDER_DESC //设置indexSort排序的顺序。
                //    }
                //}
            //]
        //},
        timeToLive: 1000000, //单位为秒。
    }
}, function (err, data) {
    if (err) {
        console.log('error:', err);
        return;
    }
    console.log('success:',data);
});

以下示例用于在创建多元索引时开启摘要与高亮。该多元索引包括k(Keyword类型)、t(Text类型)和n(Nested类型)三个字段,其中n字段包括nk(Keyword类型)、nl(Long类型)和nt(Text类型)三个子字段。同时为t字段和n字段中的nt子字段开启摘要与高亮功能。

client.createSearchIndex({
    tableName: "<TABLE_NAME>", //设置数据表名称。
    indexName: "<SEARCH_INDEX_NAME>", //设置多元索引名称。
    schema: {
        fieldSchemas: [
            {
                fieldName: "k",
                fieldType: TableStore.FieldType.KEYWORD, // 设置字段名和字段类型。
                index: true, // 设置开启索引。
                enableSortAndAgg: true, // 设置开启排序和统计功能。
                store: false,
                isAnArray: false
            },
            {
                fieldName: "t",
                fieldType: TableStore.FieldType.TEXT,
                index: true,
                enableSortAndAgg: false,
                enableHighlighting: true, //为字段开启摘要与高亮功能。
                store: true,
                isAnArray: false,
            },
            {
                fieldName: "n",
                fieldType: TableStore.FieldType.NESTED,
                index: false,
                enableSortAndAgg: false,
                store: false,
                fieldSchemas: [
                    {
                        fieldName: "nk",
                        fieldType: TableStore.FieldType.KEYWORD,
                        index: true,
                        enableSortAndAgg: true,
                        store: false,
                    },
                    {
                        fieldName: "nl",
                        fieldType: TableStore.FieldType.LONG,
                        index: true,
                        enableSortAndAgg: true,
                        store: false,
                    },
                    {
                        fieldName: "nt",
                        fieldType: TableStore.FieldType.TEXT,
                        index: true,
                        enableSortAndAgg: false,
                        enableHighlighting: true, //为字段开启摘要与高亮功能。
                        store: false,
                    },
                ]
            },
        ],
        indexSetting: { //索引的配置选项。
            "routingFields": ["id"], //只支持将主键列设置为routingFields。
            "routingPartitionSize": null
        },
        //indexSort: {//含有Nested类型的索引不支持indexSort,没有预排序。
            //sorters: [
                // { //不设置indexSort时,默认为PrimaryKeySort(升序)排序。
                //     primaryKeySort: {
                //         order: TableStore.SortOrder.SORT_ORDER_ASC
                //     }
                // },
                //{
                //   fieldSort: {
                //        fieldName: "Col_Keyword",
                //        order: TableStore.SortOrder.SORT_ORDER_DESC //设置indexSort排序的顺序。
                //    }
                //}
            //]
        //},
        timeToLive: 1000000, //单位为秒。
    }
}, function (err, data) {
    if (err) {
        console.log('error:', err);
        return;
    }
    console.log('success:',data);
});

常见问题

相关文档

  • 本页导读 (1)
  • 前提条件
  • 注意事项
  • 参数
  • 示例
  • 常见问题
  • 相关文档
AI助理

点击开启售前

在线咨询服务

你好,我是AI助理

可以解答问题、推荐解决方案等