这个坑值得记一笔。往索引里写向量时报 dimension mismatch,提示期望 768 实际收到 1024,索引是上次用另一个模型建的。

刚开始盯着「同一索引里混写了两种维度的文档」,折腾了半个多小时,排掉了。

排查方向其实有限,先把可能性排一排。换了向量模型但沿用了旧索引;mapping 里 dense_vector 的 dims 写死了旧值;模板自动创建索引时用了默认维度。

先看现有索引的向量维度

curl -s localhost:9200/docs/_mapping | jq '.docs.mappings.properties.embedding'

然后按新维度建索引并写 mapping

curl -X PUT localhost:9200/docs_v2 -H 'Content-Type: application/json' -d '{"mappings":{"properties":{"embedding":{"type":"dense_vector","dims":1024,"index":true,"similarity":"cosine"}}}}'

接着把旧数据按新模型重算后写入

python reindex.py --src docs --dst docs_v2

末了用别名切换读写流量

curl -X POST localhost:9200/_aliases -H 'Content-Type: application/json' -d '{"actions":[{"add":{"index":"docs_v2","alias":"docs"}}]}'

报错是间歇性的,我蹲守了半小时才抓到一次完整现场。

白天有业务在跑,能验证的部分都压到了晚上做。

改完先别撤,确认这几项。写入不再报维度错误,检索结果正常,业务侧通过别名无感知切换到新索引。

这里还有个坑。dense_vector 的维度建好后不能改,只能重建索引,换模型前先规划好别名切换方案。这类问题最好一次改到位,反复小改反而容易把原因搅混。

作者 9823189