RedisVL Schema 深度解析与实战指南

发布于:2025-06-28 ⋅ 阅读:(16) ⋅ 点赞:(0)

一、Schema 三大核心组成

  1. version

    • 指定 Schema 规范的版本,目前唯一支持 0.1.0,用于向后兼容。
  2. index

    • name:索引名称
    • prefix:Redis 键的前缀
    • key_separator:键名分隔符(默认 :
    • storage_type:底层存储类型,可选 hashjson
  3. fields

    • 指定要索引的字段集合,每个字段定义包含 nametype、以及可选的 attrs(属性):

      • texttagnumericgeovector 共五种类型
      • attrs 中可配置权重、排序、向量维度、向量算法等

二、IndexSchema 类介绍

IndexSchema 是 RedisVL 中的 Pydantic 模型,用于定义并管理索引 Schema:

class IndexSchema(*, index: IndexInfo, fields: Dict[str, BaseField]={}, version: Literal['0.1.0']='0.1.0')
2.1 核心方法
  • from_dict(data: Dict)
    从 Python 字典快速构建 Schema。
  • from_yaml(file_path: str)
    从 YAML 文件加载 Schema。
  • to_dict() → Dict
    将 Schema 序列化为字典,便于程序化修改或调试。
  • to_yaml(file_path: str, overwrite: bool=True)
    将 Schema 输出为 YAML 文件,用于版本控制或团队共享。
  • add_field(field_inputs: Dict)
    动态添加单个字段,常用于增量迭代。
  • add_fields(fields: List[Dict])
    批量添加多个字段。
  • remove_field(field_name: str)
    删除已有字段,支持字段重构或下线。
  • property field_names: List[str]
    返回当前所有字段名的列表,方便验证与调试。

三、Schema 定义示例

3.1 YAML 文件
version: '0.1.0'
index:
  name: user-index
  prefix: user
  key_separator: ":"
  storage_type: json

fields:
  - name: user          # 用户 ID
    type: tag
    attrs:
      separator: ","
      case_sensitive: false

  - name: credit_score  # 信用分
    type: numeric
    attrs:
      sortable: true

  - name: embedding     # 向量字段
    type: vector
    attrs:
      algorithm: flat
      dims: 512
      distance_metric: cosine
      datatype: float32
3.2 Python 字典
from redisvl.schema import IndexSchema

schema = IndexSchema.from_dict({
  "index": {
    "name": "docs-index",
    "prefix": "docs",
    "key_separator": ":",
    "storage_type": "hash",
  },
  "fields": [
    {"name": "doc-id",    "type": "tag"},
    {"name": "title",     "type": "text"},
    {"name": "views",     "type": "numeric", "attrs": {"sortable": True}},
    {"name": "location",  "type": "geo"},
    {
      "name": "doc_vector",
      "type": "vector",
      "attrs": {
        "algorithm": "hnsw",
        "dims": 1536,
        "distance_metric": "IP",
        "datatype": "float32",
        "m": 16,               # HNSW 参数
        "ef_construction": 200,# HNSW 参数
        "ef_runtime": 50       # HNSW 参数
      }
    }
  ]
})

四、动态修改 Schema

在快速迭代的业务中,经常需要增删字段或调整配置。通过 IndexSchema,我们可以:

# 添加单个字段
schema.add_field({
  "name": "status",
  "type": "tag",
  "attrs": {"case_sensitive": True}
})

# 批量添加
schema.add_fields([
  {"name": "author", "type": "text", "attrs": {"weight": 2.0}},
  {"name": "created_at", "type": "numeric"}
])

# 删除字段
schema.remove_field("views")

# 序列化检查
print(schema.field_names)  # 输出所有字段名列表

# 写出到 YAML 文件
schema.to_yaml("updated_schema.yaml", overwrite=True)

五、字段类型与可选属性

字段类型 可选属性示例 说明
text weightno_stemwithsuffixtriephonetic_matchersortable 适合全文检索,可设置词干、权重、排序等
tag separatorcase_sensitivewithsuffixtriesortable 按分隔符拆分的标签字段,可排序
numeric sortable 数值范围搜索与排序
geo sortable 地理坐标检索,可结合半径过滤
vector dimsalgorithm(flat/hnsw)
datatype
distance_metric(COSINE/IP/L2)
HNSW 特有mef_constructionef_runtimeepsilon
向量检索专属,flat 适合小规模,hnsw 支持海量高效近似检索

六、最佳实践与性能优化

  1. 版本控制:
    将 Schema YAML 文件纳入 Git 等版本控制工具,所有变更可审计、回滚。

  2. 增量演进:
    对于业务迭代,优先使用 add_field / remove_field,避免全量重建索引。

  3. 数据校验:
    在索引加载前开启 validate_on_load=True,利用 Pydantic 自动捕获类型错误,提升数据安全性。

  4. 向量索引策略:

    • 小于 10 万条向量:使用 flat 算法,精度最高
    • 超过 10 万条:优先 hnsw,并根据内存/延迟需求调整 mef_*
  5. 综合检索:
    结合 textvector 字段,实现混合检索(HybridQuery),提升复杂语义查询的效果。

  6. 排序与聚合:
    对常用字段(如时间戳、数值)开启 sortable,在分页、聚合时无需额外加载。

七、结语

通过 RedisVL 的 IndexSchema,我们可以以声明式、可编程的方式定义索引与字段,享受:

  • 灵活配置:文本、标签、数值、地理、向量字段一网打尽
  • 动态扩展:无需重启或重建,全量/增量皆可
  • 数据校验:Pydantic 驱动,提前发现脏数据
  • 性能可控:flat 与 hnsw 算法任意切换,属性精细优化

掌握好 Schema 设计,便是构建可维护、高性能搜索与推荐系统的基石。希望本文能帮助你在 RedisVL 的世界里,游刃有余地打造下一代检索引擎!


网站公告

今日签到

点亮在社区的每一天
去签到