过滤是 Chroma 里最容易被低估的部分。向量检索只能回答“跟这句话像的有哪些”,真实业务里你几乎总要再加一句“而且要 2024 年之后的”、“而且只要售后类的”、“而且必须包含'退款'两个字”。这一章把 where 和 where_document 讲透。
下面先列出 Chroma 支持的运算符,从整体上进行把握,也可作为查询手册。
where 用于过滤 metadata:
| 运算符 | 含义 | 适用类型 | 示例 |
| (直接给值) | 等于 | str / int / float / bool | {"category": "售后"} |
| $eq | 等于 | str / int / float / bool | {"year": {"$eq": 2025}} |
| $ne | 不等于 | str / int / float / bool | {"category": {"$ne": "内部"}} |
| $gt | 大于 | int / float | {"score": {"$gt": 0.7}} |
| $gte | 大于等于 | int / float | {"score": {"$gte": 0.5}} |
| $lt | 小于 | int / float | {"year": {"$lt": 2026}} |
| $lte | 小于等于 | int / float | {"year": {"$lte": 2025}} |
| $in | 在列表里 | str / int / float / bool | {"tag": {"$in": ["合同","工单"]}} |
| $nin | 不在列表里 | str / int / float / bool | {"cat": {"$nin": ["测试","草稿"]}} |
| $contains | 数组包含某元素 | 数组字段 | {"tags": {"$contains": "AI"}} |
| $not_contains | 数组不包含某元素 | 数组字段 | {"tags": {"$not_contains": "废弃"}} |
| $and | 且(列表里所有条件都要满足) | — | {"$and": [{"score":{"$gt":0.6}},{"cat":"售后"}]} |
| $or | 或(列表里任一条件满足) | — | {"$or": [{"cat":"合同"},{"cat":"工单"}]} |
where_document 用于过滤 document 文本:
| 运算符 | 含义 | 示例 |
| $contains | 包含子串 | {"$contains": "退款申请"} |
| $not_contains | 不包含子串 | {"$not_contains": "作废"} |
| $regex | 匹配正则 | {"$regex": "^订单.*"} |
| $not_regex | 不匹配正则 | {"$not_regex": "测试\\d+"} |
| $and / $or | 组合 | {"$and": [{"$contains":"退款"},{"$not_contains":"驳回"}]} |
请看后续小节,将逐步展开。