当前位置: 首页 > news >正文

RedisVL Schema 官方手册详读

1 Schema 三大组件

组件说明
versionSchema 规范版本,当前仅支持 0.1.0
index索引级配置:name / prefix / key_separator / storage_type
fields需要写入索引的字段集合及其自定义属性

2 IndexSchema 类总览

class IndexSchema(*, index: IndexInfo,                     # 基础索引信息fields: Dict[str, BaseField] = {},    # 字段集合version: Literal["0.1.0"] = "0.1.0",  # 规范版本
)
2.1 主要用途
  • 声明式管理 倒排 + 向量 混合索引
  • 支持 YAML / Dict 双格式创建
  • 提供动态增删字段、序列化、验证等操作
2.2 创建方式
from redisvl.schema import IndexSchema# YAML
schema = IndexSchema.from_yaml("schema.yaml")# Dict
schema = IndexSchema.from_dict({"index": { ... },"fields": [ ... ]
})
2.3 关键方法 & 属性
方法 / 属性作用抛出
add_field(field_inputs)追加单字段ValueError(重名 / 缺必填)
add_fields(fields)批量追加字段列表ValueError(重名)
remove_field(name)删除指定字段
from_dict(data) / from_yaml(path)构建 Schema
to_dict() / to_yaml(path, overwrite=True)序列化为 Dict / 写入 YAMLFileExistsError(overwrite=False 且已存在)
field_names (property)所有字段名列表
fields (attr)Dict[str, BaseField] 字段映射
index (attr)IndexInfo 索引信息
version (attr)'0.1.0'

3 Schema 示例

3.1 YAML
version: '0.1.0'index:name: user-indexprefix: userkey_separator: ":"storage_type: jsonfields:- name: usertype: tag- name: credit_scoretype: tag- name: embeddingtype: vectorattrs:algorithm: flatdims: 3distance_metric: cosinedatatype: float32
3.2 Python Dict
index_schema_dict = {"index": {"name": "user-index","prefix": "user","key_separator": ":","storage_type": "json",},"fields": [{"name": "user", "type": "tag"},{"name": "credit_score", "type": "tag"},{"name": "embedding","type": "vector","attrs": {"algorithm": "flat","dims": 3,"distance_metric": "cosine","datatype": "float32"}}]
}
schema = IndexSchema.from_dict(index_schema_dict)

注意fields字段名必须唯一,否则会触发 ValueError

4 动态增删字段示例

# 新增单个 TAG 字段
schema.add_field({"name": "user", "type": "tag"})# 新增向量字段
schema.add_field({"name": "user-embedding","type": "vector","attrs": {"dims": 1024, "algorithm": "flat", "datatype": "float32"}
})# 批量追加
schema.add_fields([{"name": "bio", "type": "text"},{"name": "age-vec","type": "vector","attrs": {"dims": 256, "algorithm": "flat", "datatype": "float32"}}
])# 删除字段
schema.remove_field("credit_score")

5 字段定义速查

字段类型必填键可选键 (attrs)
textname, type=textweight | no_stem | withsuffixtrie | phonetic_matcher | sortable
tagname, type=tagseparator | case_sensitive | withsuffixtrie | sortable
numericname, type=numericsortable
geoname, type=geosortable
vector (通用)name, type=vectordims · algorithm(flat/hnsw) · datatype(bfloat16/float16/float32/float64) · distance_metric(COSINE/L2/IP)
vector (HNSW 追加)m · ef_construction · ef_runtime · epsilon
路径与排序示例
- name: titletype: textpath: $.document.titleattrs:weight: 1.0withsuffixtrie: true- name: locationtype: geoattrs:sortable: true

6 序列化 / 反序列化

# 保存为 YAML
schema.to_yaml("schema_out.yaml")# 读取再转 Dict
reloaded = IndexSchema.from_yaml("schema_out.yaml")
print(reloaded.to_dict())

7 常见报错对照

报错信息常见原因
ValueError: name already exists字段重名
ValidationError (pydantic)字段缺必填 / 类型不符
FileExistsErrorto_yaml(..., overwrite=False) 且文件已存在
dims missing / datatype not supportedVECTOR attrs 参数不完整或非法

8 更多资料

  • Redis FT.CREATE 官方字段选项:https://redis.io/commands/ft.create/
  • RedisVL 源码 & 文档:https://github.com/redis/redis-vl-python

借助 IndexSchema,你可以在 YAML 或纯 Python 中 声明式 描述搜索索引,轻松完成索引创建、校验、版本管理与字段扩展,真正做到“配置即索引、脚本零改动”。

相关文章:

  • 用户行为序列建模(篇六)-【阿里】DSIN
  • BF的数据结构题单-省选根号数据结构 - 题单 - 洛谷 计算机科学教育新生态
  • SQL Server从入门到项目实践(超值版)读书笔记 19
  • 03【C++ 入门基础】函数重载
  • 使用ros2服务实现人脸检测4-客户端(适合0基础小白)
  • 通达信【MACD趋势增强系统】幅图(含支撑压力位)
  • D-FiNE:在DETR模型中重新定义回归任务为精细粒度分布细化
  • MySQL数据库的增删改查
  • SpringCloud系列(41)--SpringCloud Config分布式配置中心简介
  • 模拟多维物理过程与基于云的数值分析-AI云计算数值分析和代码验证
  • CppCon 2017 学习:The Asynchronous C++ Parallel Programming Model
  • 在线之家官网入口 - 免费高清海外影视在线观看平台
  • STM32之28BYJ-48步进电机驱动
  • 思二勋:算法稳定币的发展在于生态场景、用户和资产的丰富性
  • 打造地基: App拉起基础小程序容器
  • 大事件项目记录12-文章管理接口开发-总
  • 现代 JavaScript (ES6+) 入门到实战(一):告别 var!拥抱 let 与 const,彻底搞懂作用域
  • Spark Web UI从0到1详解
  • SpringSecurity6-授权-动态权限
  • (NIPS-2024)CogVLM:预训练语言模型的视觉专家