先把当时的情况摆出来。Elasticsearch 8.15 的 dense_vector 映射 dims 写的 768,模型换成 text-embedding-3-large 输出 3072 维,bulk 写入报 the number of dimensions in the vector is not equal to the number of dimensions in the mapping。

排查方向其实有限,先把可能性排一排。mapping 建好之后维度不能修改。还有一种是索引模板里还留着旧的 dims 值。

另外,bulk 请求部分成功,错误被吞掉。少数情况下是别名指向的仍是旧索引。

动手时先看当前索引的 mapping 和别名

curl -s 'localhost:9200/kb/_mapping?pretty'

随后按新维度新建索引并写模板

curl -s -X PUT 'localhost:9200/kb_v2' -H 'Content-Type: application/json' -d @mapping.json

再往下用 reindex 把旧数据迁过去再补新向量

curl -s -X POST 'localhost:9200/_reindex' -H 'Content-Type: application/json' -d @reindex.json

等前面都确认过,再切换别名后抽查一次 kNN 查询

curl -s 'localhost:9200/kb/_search' -H 'Content-Type: application/json' -d @knn.json

排查用的命令都是临时敲的,没有写成脚本,事后才整理。

手上没有现成的复现脚本,就照着线上请求手工拼了一个。

怎么算真的好了,看的是同一批现象。bulk 写入错误为 0,查询能返回相似文档,别名指向 kb_v2 且 mapping 里的 dims 是 3072。

提醒一点。dense_vector 的维度是映射级别的硬约束,改模型必须新建索引,reindex 只迁数据不迁向量,新向量要重算一遍。改动前把原配置备份一份,出问题能马上回滚,这比任何技巧都实用。

作者 9823189