搜索本产品文档关键词
Index 操作
所有文档
menu
没有找到结果,请重新输入

向量数据库 VectorDB

Index 操作

创建索引

功能介绍

为指定表和指定字段新建索引,当前仅支持新建向量索引

请求示例

import pymochow
from pymochow.configuration import Configuration
from pymochow.auth.bce_credentials import BceCredentials
from pymochow.model.schema import SecondaryIndex, VectorIndex, HNSWParams, AutoBuildPeriodical
from pymochow.model.enum import IndexType, MetricType

account = 'root'
api_key = '$您的账户API密钥'
endpoint = '$您的实例访问端点' # 例如:'http://127.0.0.1:5287'

config = Configuration(credentials=BceCredentials(account, api_key),
            endpoint=endpoint)
client = pymochow.MochowClient(config)

db = client.database("db_test")

indexes = []
indexes.append(
    VectorIndex(
        index_name="vector_idx",
        index_type=IndexType.HNSW,
        field="vector",
        metric_type=MetricType.L2,
        params=HNSWParams(m=32, efconstruction=200),
        auto_build=True,
        auto_build_index_policy=AutoBuildPeriodical(5000, "2026-01-01 12:00:00")
    )
)

table = db.table("book_vector")
table.create_indexes(indexes)

client.close()

请求参数

参数 参数类型 是否必选 参数含义
indexes List<Index> 索引列表。

Index参数

请参见建表操作的索引参数描述。


删除索引

功能介绍

删除指定索引,当前不支持删除构建中的向量索引。

请求示例

import pymochow
from pymochow.configuration import Configuration
from pymochow.auth.bce_credentials import BceCredentials

account = 'root'
api_key = '$您的账户API密钥'
endpoint = '$您的实例访问端点' # 例如:'http://127.0.0.1:5287'

config = Configuration(credentials=BceCredentials(account, api_key),
            endpoint=endpoint)
client = pymochow.MochowClient(config)

db = client.database("db_test")

table = db.table("book_vector")
table.drop_index("vector_idx")

client.close()

请求参数

参数 参数类型 是否必选 参数含义
index_name String 指定索引的名称。

重建向量索引

功能介绍

重建指定索引,当前仅支持重建向量索引。

请求示例

import pymochow
from pymochow.configuration import Configuration
from pymochow.auth.bce_credentials import BceCredentials

account = 'root'
api_key = '$您的账户API密钥'
endpoint = '$您的实例访问端点' # 例如:'http://127.0.0.1:5287'

config = Configuration(credentials=BceCredentials(account, api_key),
            endpoint=endpoint)
client = pymochow.MochowClient(config)

db = client.database("db_test")

table = db.table("book_vector")
table.rebuild_index(index_name="vector_idx")

client.close()

请求参数

参数 参数类型 是否必选 参数含义
index_name String 向量索引的名称。

查询索引详情

功能介绍

查询指定索引的详情。

请求示例

import pymochow
from pymochow.configuration import Configuration
from pymochow.auth.bce_credentials import BceCredentials

account = 'root'
api_key = '$您的账户API密钥'
endpoint = '$您的实例访问端点' # 例如:'http://127.0.0.1:5287'

config = Configuration(credentials=BceCredentials(account, api_key),
            endpoint=endpoint)
client = pymochow.MochowClient(config)

db = client.database("db_test")

table = db.table("book_vector")
index = table.describe_index(index_name="vector_idx")

client.close()

请求参数

参数 参数类型 是否必选 参数含义
index_name String 指定索引的名称。

返回参数

参数 参数类型 参数含义
index Index Index对象

Index参数

参数 参数类型 参数含义
vector_index VectorIndex 向量索引对象。
secondary_index SecondaryIndex 标量二级索引对象。
filtering_index FilteringIndex 过滤索引对象。在带有过滤条件的检索场景中,为过滤的标量字段添加索引,可以显著加速检索过程,从而有效提升检索性能。
inverted_index InvertedIndex 倒排索引对象。

VectorIndex参数

参数 参数类型 参数含义
index_name String 索引名称。
index_type IndexType 索引类型。
field String 索引作用于的字段名称。
metric_type MetricType 向量之间距离度量算法类型。取值如下:
  • L2:欧几里得距离
  • IP:内积距离
  • COSINE:余弦距离

    注:当使用COSINE距离时,用户需要自行对相关向量进行归一化操作,未经归一化的向量将导致search结果不准确
  • autoBuild Bool 是否有自动构建索引策略。
    autoBuildPolicy AutoBuildPolicy 自动构建索引策略参数
  • policyType:策略类型,有如下几种类型:
    periodical,周期性构建索引。
    rowCountIncrement,根据tablet行增长数自动构建索引。
    timing,定时构建索引
  • periodInSecond:周期性构建索引的秒数,只在周期性构建索引策略类型时返回。
  • timing:字符串类型,返回定时构建的时间,只在定时构建索引策略类型时返回
  • rowCountIncrement:返回触发构建时增长的行数以及百分比,只在行增长数构建索引类型时返回。
  • params Params 向量索引构建参数。
    state IndexState 索引状态。取值如下:
  • BUILDING:表示索引正在构建中
  • NORMAL:表示索引已完成构建并处于正常状态
  • SecondaryIndex参数

    参数 参数类型 参数含义
    index_name String 索引名称
    field String 索引作用于的字段名称。

    FilteringIndex

    参数 参数类型 参数含义
    index_name String 索引名称
    fields List 索引作用于的字段名称。

    InvertedIndex

    参数 参数类型 参数含义
    index_name String 索引名称
    fields List 索引作用于的字段名称。
    params InvertedIndexParams 倒排索引参数。
    field_attributes List 指定建立倒排索引的列是否需要分词(默认是会分词),参数顺序应与'fields'里列名一一对应。目前支持以下选项:
  • ATTRIBUTE_ANALYZED
  • ATTRIBUTE_NOT_ANALYZED
  • InvertedIndexParams

    参数 参数类型 参数含义
    analyzer InvertedIndexAnalyzer 指定倒排索引的分词器。 目前支持以下三种:
  • ENGLISH_ANALYZER : 英文分词器
  • CHINESE_ANALYZER: 中文分词器
  • DEFAULT_ANALYZER: 默认分词器,适用于英文、中文、中英文混合等场景,建议使用
  • parse_mode InvertedIndexParseMode 分词器的分词模式。
  • COARSE_MODE: 较粗粒度,基于不产生歧义的较大粒度进行切分,适宜于对语义表达能力要求较高的应用
  • FINE_MODE: 细粒度模式,基于语义完整的最小粒度进行切分
  • 修改索引

    功能介绍

    修改向量索引信息,目前只支持修改autoBuild属性

    请求示例

    import pymochow
    import time
    
    from pymochow.configuration import Configuration
    from pymochow.auth.bce_credentials import BceCredentials
    from pymochow.model.schema import Schema, Field, SecondaryIndex, VectorIndex, HNSWParams, AutoBuildTiming, AutoBuildPeriodical, AutoBuildRowCountIncrement
    from pymochow.model.enum import FieldType, IndexType, MetricType, ServerErrCode
    from pymochow.model.enum import TableState, IndexState
    from pymochow.model.table import Partition, Row, AnnSearch, HNSWSearchParams
    
    if __name__ == "__main__":
        account = 'root'
        api_key = '$您的API密钥'
        endpoint = '$您的实例端点' #例如:'http://127.0.0.1:5287'
        config = Configuration(credentials=BceCredentials(account, api_key),
                endpoint=endpoint)
        client = pymochow.MochowClient(config)
        database = 'book'
        table_name = 'book_segments'
    
        db = client.create_database(database)
        fields = []
        fields.append(Field("id", FieldType.STRING, primary_key=True,
            partition_key=True, auto_increment=False, not_null=True))
        fields.append(Field("bookName", FieldType.STRING, not_null=True))
        fields.append(Field("vector", FieldType.FLOAT_VECTOR, not_null=True, dimension=3))
    
        db.create_table(
            table_name=table_name,
            replication=2,
            partition=Partition(partition_num=3),
            schema=Schema(fields=fields, indexes=[])
        )
        while True:
            time.sleep(2)
            table = db.describe_table(table_name)
            if table.state == TableState.NORMAL:
                break
    
        table = db.table('book_segments')
        indexes = []
        vindex = VectorIndex(index_name="vector_idx",
                index_type=IndexType.HNSW,
                field="vector", metric_type=MetricType.L2,
                params=HNSWParams(m=32, efconstruction=200))
        indexes.append(vindex)
        table.create_indexes(indexes)
        table.modify_index(index_name="vector_idx", auto_build=True,
                           auto_build_index_policy=AutoBuildTiming("2024-06-06 00:00:00"))

    请求参数

    参数 参数类型 是否必选 参数含义
    index_name String 索引列表。
    auto_build Boolean 是否自动构建索引,默认为False。
    auto_build_index_policy AutoBuildPolicy 自动构建索引策略,当前支持如下策略:
  • AutoBuildTiming:定时构建,指定构建的时间,构建一次,不会重复构建。例如AutoBuildTiming("2026-09-11 23:07:00"),时间格式支持UTC及LOCAL。
  • AutoBuildPeriodical:周期性构建,每过period_s秒构建一次索引,可重复构建。可以指定从某个时间点开始,例如AutoBuildPeriodical(24 * 3600, "2026-09-11 23:07:00")。周期不能低于3600,时间格式支持LOCAL以及UTC。
  • AutoBuildRowCountIncrement:增量行数构建。Tablet(不是table)增加或者减少指定的行数时会自动构建一次索引,可重复构建,支持具体行数以及百分比,只需传入一种即可,也可传入两种,触发其中之一便会开始构建。例如AutoBuildRowCountIncrement(row_count_increment = 10000, row_count_increment_ratio = 0.5)。增量行数不低于10000,增量行数百分比需要大于0。
  • 上一篇
    Row 操作
    下一篇
    Go SDK