民政网站建设情况汇报,南京建设网站公司哪家好,禁漫天入口18comic,.win域名做网站怎么样在向量搜索中#xff0c;过滤#xff08;Filtering#xff09; 是保证结果精准性和业务契合度的关键手段。Qdrant 的过滤机制不仅能在向量相似度检索的基础上叠加结构化条件#xff0c;还提供了灵活的布尔逻辑组合#xff0c;让我们可以像写数据库查询一样#xff0c;精准…
在向量搜索中过滤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, port6333)search_result  client.search(collection_nameproducts,query_vector[0.1, 0.2, 0.3, 0.4],limit5,query_filterFilter(must[FieldCondition(keycity, matchMatchValue(valueLondon)),FieldCondition(keyprice, rangeRange(lte1000)),FieldCondition(keybrand, matchMatchValue(valueApple))])
)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_nameproducts,query_vector[0.1, 0.2, 0.3, 0.4],limit5,query_filterFilter(must[FieldCondition(keycity, matchMatchValue(valueLondon))],should[FieldCondition(keybrand, matchMatchValue(valueApple))])
)
2.3 must_not — 排除条件NOT定义列表中的条件必须全部不成立。逻辑等价于 NOT。JSON 示例
filter: {must_not: [{ key: brand, match: { value: Asus } }]
}解释排除 Asus 品牌。Python 实操
search_result  client.search(collection_nameproducts,query_vector[0.1, 0.2, 0.3, 0.4],limit5,query_filterFilter(must_not[FieldCondition(keybrand, matchMatchValue(valueAsus))])
)
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_nameproducts,query_vector[0.1, 0.2, 0.3, 0.4],limit5,query_filterFilter(should[FieldCondition(keyfeature, matchMatchValue(valuetouchscreen)),FieldCondition(keyfeature, matchMatchValue(valuessd)),FieldCondition(keyfeature, matchMatchValue(valuebacklit_keyboard))],min_shouldMinShould(min_count2))
)4. 总结must  全部必须成立ANDshould  无 must 时是 OR有 must 时影响排序must_not  必须全部不成立NOTmin_should  要求 should 中命中的最小数量
在实际业务中可以先用简单的 must / should / must_not 组合调试逻辑再引入嵌套和 min_should 做更复杂的检索策略。