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 做更复杂的检索策略。

相关推荐
喵手20 小时前
Python爬虫零基础入门【第九章:实战项目教学·第15节】搜索页采集:关键词队列 + 结果去重 + 反爬友好策略!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·搜索页采集·关键词队列
Suchadar20 小时前
if判断语句——Python
开发语言·python
ʚB҉L҉A҉C҉K҉.҉基҉德҉^҉大20 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
喵手20 小时前
Python爬虫零基础入门【第九章:实战项目教学·第14节】表格型页面采集:多列、多行、跨页(通用表格解析)!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·表格型页面采集·通用表格解析
0思必得021 小时前
[Web自动化] 爬虫之API请求
前端·爬虫·python·selenium·自动化
莫问前路漫漫21 小时前
WinMerge v2.16.41 中文绿色版深度解析:文件对比与合并的全能工具
java·开发语言·python·jdk·ai编程
木头左21 小时前
Backtrader框架下的指数期权备兑策略资金管理实现与风险控制
python
玄同76521 小时前
LangChain 核心组件全解析:构建大模型应用的 “乐高积木”
人工智能·python·语言模型·langchain·llm·nlp·知识图谱
喵手21 小时前
Python爬虫实战:从零构建 Hacker News 数据采集系统:API vs 爬虫的技术抉择!(附CSV导出 + SQLite 存储)!
爬虫·python·爬虫实战·hacker news·python爬虫工程化实战·零基础python爬虫教学·csv导出
测试老哥21 小时前
软件测试之功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例