Files
life-echo/api/.agents/skills/redis-development/rules/vector-algorithm-choice.md
Sully 53e0065e3e refactor(api): TOML 配置 SSOT、统一错误契约、Auth/事务加固与可观测性 (#33)
配置 SSOT(TOML + .env)
统一错误契约
Auth 与事务边界
Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client
可观测性(OpenTelemetry + LGTM)
2026-05-22 13:44:50 +08:00

2.1 KiB

title, impact, impactDescription, tags, description, alwaysApply
title impact impactDescription tags description alwaysApply
Choose HNSW vs FLAT Based on Requirements HIGH HNSW trades accuracy for speed, FLAT provides exact results vector, hnsw, flat, algorithm, performance Choose HNSW vs FLAT Based on Requirements true

Choose HNSW vs FLAT Based on Requirements

Select the right algorithm based on your accuracy requirements and dataset size.

Algorithm Speed Accuracy Memory Best For
HNSW Fast (approximate) ~95%+ recall tunable Higher Large datasets (>10k vectors)
FLAT Slower (exact) 100% (exact) Lower Small datasets, accuracy-critical

Correct: Use HNSW for large-scale production workloads.

from redisvl.schema import IndexSchema

# HNSW - fast approximate search, tunable accuracy
schema = IndexSchema.from_dict({
    "index": {"name": "idx:docs", "prefix": "doc:"},
    "fields": [
        {"name": "embedding", "type": "vector", "attrs": {
            "dims": 1536,
            "algorithm": "HNSW",
            "distance_metric": "COSINE",
            "datatype": "FLOAT32",
            "m": 16,                      # Higher = more accurate, more memory
            "ef_construction": 200        # Higher = better index quality, slower build
        }}
    ]
})

Correct: Use FLAT when exact results are required.

# FLAT - exact brute-force search, guaranteed accuracy
schema = IndexSchema.from_dict({
    "index": {"name": "idx:small", "prefix": "small:"},
    "fields": [
        {"name": "embedding", "type": "vector", "attrs": {
            "dims": 1536,
            "algorithm": "FLAT",
            "distance_metric": "COSINE"
        }}
    ]
})

Tuning HNSW accuracy vs speed:

  • M: Connections per node (16-64). Higher = better recall, more memory
  • EF_CONSTRUCTION: Build-time parameter (100-500). Higher = better graph quality
  • EF_RUNTIME: Query-time parameter. Higher = better recall, slower queries

Reference: Redis Vector Search