Qdrant Filtering:must / should / must_not 全解析(含 Python 实操)

在向量搜索中,过滤(Filtering) 是保证结果精准性和业务契合度的关键手段。

Qdrant 的过滤机制不仅能在向量相似度检索的基础上叠加结构化条件,还提供了灵活的布尔逻辑组合,让我们可以像写数据库查询一样,精准控制搜索范围。

本文将深入解析 Qdrant 的过滤规则,并结合 Python 实例演示 must、should、must_not 的用法。


1. 过滤机制的意义

向量检索只考虑语义相似度,但在实际业务中往往需要额外的约束:

  • 电商:只展示"价格低于 1000 元"的笔记本

  • 招聘:只匹配"3 年以上经验"的候选人

  • 地图搜索:只返回"当前城市"的餐厅

Qdrant 的 Filtering 就是为这些结构化条件而生的。


2. 三大核心关键字

2.1 must --- 必须满足的条件(AND)

  • 定义:列表中的所有条件都必须成立。

  • 逻辑:等价于 AND。

JSON 示例:

复制代码
"filter": {
  "must": [
    { "key": "city", "match": { "value": "London" } },
    { "key": "price", "range": { "lte": 1000 } },
    { "key": "brand", "match": { "value": "Apple" } }
  ]
}

解释:只返回满足

city = London 且 price ≤ 1000 且 brand = Apple 的结果。

Python 实操:

python 复制代码
from qdrant_client import QdrantClient
from qdrant_client.models import Filter, FieldCondition, MatchValue, Range

client = QdrantClient("localhost", port=6333)

search_result = client.search(
    collection_name="products",
    query_vector=[0.1, 0.2, 0.3, 0.4],
    limit=5,
    query_filter=Filter(
        must=[
            FieldCondition(key="city", match=MatchValue(value="London")),
            FieldCondition(key="price", range=Range(lte=1000)),
            FieldCondition(key="brand", match=MatchValue(value="Apple"))
        ]
    )
)

print(search_result)

2.2 should --- 可选条件(OR / 排序加权)

  • 定义:

    • 有 must 时:should 条件不满足也会返回,但满足的结果会排前。

    • 无 must 时:should 至少要有一个条件成立(OR 逻辑)。

JSON 示例:must + should

复制代码
"filter": {
  "must": [
    { "key": "city", "match": { "value": "London" } }
  ],
  "should": [
    { "key": "brand", "match": { "value": "Apple" } }
  ]
}

解释:必须在伦敦;Apple 品牌排前,不是 Apple 也会返回。

Python 实操:

python 复制代码
from qdrant_client.models import ShouldCondition

search_result = client.search(
    collection_name="products",
    query_vector=[0.1, 0.2, 0.3, 0.4],
    limit=5,
    query_filter=Filter(
        must=[FieldCondition(key="city", match=MatchValue(value="London"))],
        should=[FieldCondition(key="brand", match=MatchValue(value="Apple"))]
    )
)

2.3 must_not --- 排除条件(NOT)

  • 定义:列表中的条件必须全部不成立。

  • 逻辑:等价于 NOT。

JSON 示例:

复制代码
"filter": {
  "must_not": [
    { "key": "brand", "match": { "value": "Asus" } }
  ]
}

解释:排除 Asus 品牌。

Python 实操:

python 复制代码
search_result = client.search(
    collection_name="products",
    query_vector=[0.1, 0.2, 0.3, 0.4],
    limit=5,
    query_filter=Filter(
        must_not=[FieldCondition(key="brand", match=MatchValue(value="Asus"))]
    )
)

3. min_should 高级用法

min_should 可要求 should 中必须满足最少数量。

JSON 示例:至少满足 2 个特性

复制代码
"filter": {
  "should": [
    { "key": "feature", "match": { "value": "touchscreen" } },
    { "key": "feature", "match": { "value": "ssd" } },
    { "key": "feature", "match": { "value": "backlit_keyboard" } }
  ],
  "min_should": {
    "min_count": 2
  }
}

Python 实操:

python 复制代码
from qdrant_client.models import Filter, FieldCondition, MatchValue, MinShould

search_result = client.search(
    collection_name="products",
    query_vector=[0.1, 0.2, 0.3, 0.4],
    limit=5,
    query_filter=Filter(
        should=[
            FieldCondition(key="feature", match=MatchValue(value="touchscreen")),
            FieldCondition(key="feature", match=MatchValue(value="ssd")),
            FieldCondition(key="feature", match=MatchValue(value="backlit_keyboard"))
        ],
        min_should=MinShould(min_count=2)
    )
)

4. 总结

  • must = 全部必须成立(AND)

  • should = 无 must 时是 OR;有 must 时影响排序

  • must_not = 必须全部不成立(NOT)

  • min_should = 要求 should 中命中的最小数量

在实际业务中,可以先用简单的 must / should / must_not 组合调试逻辑,再引入嵌套和 min_should 做更复杂的检索策略。

相关推荐
茉莉玫瑰花茶26 分钟前
综合案例 - AI 智能租房助手 [ 5 ]
服务器·数据库·人工智能·python·ai
文艺倾年33 分钟前
【强化学习】强化学习基本概念,20W字总结(一)
人工智能·python·语言模型·自然语言处理·面试·职场和发展·大模型
宸丶一36 分钟前
Day 13:持久化记忆 - 让 Agent 拥有长期记忆
jvm·python·ai
码云骑士1 小时前
13-列表append的底层真相(上)-listobject源码中的预分配策略
开发语言·python
浦信仿真大讲堂1 小时前
达索系统SIMULIA Abaqus 2026接触和约束的增强新功能介绍
人工智能·python·算法·仿真软件·达索软件
xufengzhu2 小时前
第三方 Python 库 Loguru 的进阶实战
python·loguru
极光代码工作室2 小时前
基于深度学习的手写数字识别系统
人工智能·python·深度学习·神经网络·机器学习
geovindu3 小时前
python: speech to text offline
开发语言·python·语音识别
AI创界者3 小时前
告别云端限制!Sulphur 2 本地文生视频/图生视频整合包,本地部署,解压即用,保姆级部署与工作流实战
人工智能·python·aigc·音视频
tsfy20033 小时前
Python批量调整Excel格式,并排版导出PDF
python·pdf·excel