Vespa DB近邻搜索报错:query(q_embedding)应为张量而非字符串
Vespa近邻搜索报错:query(q_embedding)被识别为字符串而非张量
问题场景
使用Vespa DB对embeddings执行nearestNeighbor近邻搜索时,按官方文档构造查询请求,尝试从products文档集检索结果,返回类型不匹配错误,且Vespa容器未生成错误日志:
Expected "query(q_embedding)" to be a tensor, but it is the string
原始请求、响应及Schema
原始请求
{ "yql": "SELECT * FROM products WHERE ([{\"targetHits\":10, \"approximate\": true}]nearestNeighbor(text_embedding, q_embedding))", "ranking": { "profile": "default", "listFeatures": true, "features": { "query(q_embedding)": [ 0.06836861371994019, 0, 0.005350692197680473, // 省略剩余数值 0 ] } } }
错误响应
{ "root": { "id": "toplevel", "relevance": 1.0, "fields": { "totalCount": 0 }, "errors": [ { "code": 4, "summary": "Invalid query parameter", "source": "products", "message": "Expected 'query(q_embedding)' to be a tensor, but it is the string '[\n 0.06836861371994019,\n 0,\n ...(省略部分数值)\n ]'" } ] } }
Schema定义
schema products { document products { field id type int { } field gender type string { indexing: summary | attribute } field masterCategory type string { indexing: summary | attribute } field subCategory type string { indexing: summary | attribute } field articleType type string { indexing: summary | attribute } field baseColour type string { indexing: summary | attribute } field season type string { indexing: summary | attribute } field year type int { indexing: summary | attribute } field usage type string { indexing: summary | attribute } field productDisplayName type string { indexing: summary | attribute } field image type string { indexing: summary | attribute } field image_embedding type tensor<float>(x[128]) { indexing: attribute attribute { distance-metric: euclidean } } field text_embedding type tensor<float>(x[128]) { indexing: attribute attribute { distance-metric: euclidean } } } fieldset default { fields: id, gender, masterCategory, subCategory, articleType, baseColour, season, year, usage, productDisplayName, image, image_embedding, text_embedding } rank-profile default { first-phase { expression: closeness(field, image_embedding) + closeness(field, text_embedding) } } }
错误原因
Vespa要求查询中的张量特征必须显式声明张量类型,与Schema中定义的tensor<float>(x[128])结构完全匹配。原始请求直接传递数值数组,被Vespa解析为字符串类型,导致类型不匹配报错。
修复方案
将query(q_embedding)的数值数组包装为张量对象,明确指定type为tensor<float>(x[128]),values字段保留原始数值数组。
修改后的请求
{ "yql": "SELECT * FROM products WHERE ([{\"targetHits\":10, \"approximate\": true}]nearestNeighbor(text_embedding, q_embedding))", "ranking": { "profile": "default", "listFeatures": true, "features": { "query(q_embedding)": { "type": "tensor<float>(x[128])", "values": [ 0.06836861371994019, 0, 0.005350692197680473, // 省略剩余数值 0 ] } } } }
验证
修改请求后重新发送,Vespa会正确识别q_embedding为张量类型,执行近邻搜索并返回匹配的文档列表,错误提示消失。
内容的提问来源于stack exchange,提问作者Rogue
相关产品推荐
相关产品推荐

