在向量搜索中,過濾(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 實操:
from qdrant_client import QdrantClient
from qdrant_client.models import Filter, FieldCondition, MatchValue, Rangeclient = 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 實操:
from qdrant_client.models import ShouldConditionsearch_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 實操:
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 實操:
from qdrant_client.models import Filter, FieldCondition, MatchValue, MinShouldsearch_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 做更復雜的檢索策略。