混合检索
更新时间 2025-07-25 12:04:42
最近更新时间: 2025-07-25 12:04:42
本文为您介绍天翼云云搜索服务的混合检索能力及使用方法。
功能简介
混合检索(Hybrid Search)是天翼云云搜索服务的核心增强功能,支持在同一查询中同时执行关键词检索(BM25)和向量相似度搜索。该能力通过融合传统搜索的精准匹配与AI模型的向量化能力,显著提升复杂场景的搜索质量,尤其适用于电商搜索、内容推荐、多模态搜索等需兼顾文本相关性与语义相似性的场景。
注意
目前只有OpenSearch类型实例支持混合检索功能。
使用示例
删除旧索引
DELETE /hybrid-products
创建新索引
1分片0副本 + 8维向量
PUT /hybrid-products
{
"settings": {
"index": {
"knn": true,
"knn.algo_param.ef_search": 200,
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"product_id": { "type": "keyword" },
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"feature_vector": {
"type": "knn_vector",
"dimension": 8
},
"price": { "type": "float" }
}
}
}
插入数据
新增三条产品数据
POST /hybrid-products/_doc/101
{
"product_id": "P12345",
"title": "金属便携咖啡杯",
"feature_vector": [0.12, 0.23, 0.34, 0.45, 0.56, 0.67, 0.78, 0.89],
"price": 89.9
}
POST /hybrid-products/_doc/102
{
"product_id": "P22357",
"title": "不锈钢保温杯带茶隔",
"feature_vector": [0.09, 0.21, 0.37, 0.48, 0.51, 0.62, 0.74, 0.81],
"price": 69.0
}
POST /hybrid-products/_doc/103
{
"product_id": "P33489",
"title": "陶瓷茶杯礼盒装",
"feature_vector": [0.33, 0.44, 0.55, 0.66, 0.77, 0.88, 0.99, 0.10],
"price": 129.9
}
执行混合检索查询
POST /hybrid-products/_search?pretty
{
"size": 1,
"query": {
"hybrid": {
"queries": [
{
"match": {
"title": {
"query": "不锈钢保温杯",
"boost": 0.8
}
}
},
{
"knn": {
"feature_vector": {
"vector": [0.12, 0.23, 0.34, 0.45, 0.56, 0.67, 0.78, 0.89],
"k": 50,
"boost": 1.2
}
}
}
]
}
},
"_source": ["product_id", "title", "price"]
}
混合检索查询结果示例
{
"took" : 253,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 3.6675835,
"hits" : [
{
"_index" : "hybrid-products",
"_id" : "102",
"_score" : -9.549512E9,
"_source" : {
"price" : 69.0,
"product_id" : "P22357",
"title" : "不锈钢保温杯带茶隔"
}
}
]
}
}