Elasticsearch不仅仅是一个数据库;除其他功能外,它还是一个矢量数据库。当索引密集向量时,Elasticsearch会维护一种特殊的数据结构,称为分层导航小世界(HNSW)图。这种结构能够在搜索期间实现快速近似k近邻(kNN)查找。
该图中矢量的排列是基于矢量之间的相似性。默认情况下,Elasticsearch为此使用余弦相似性。如本文所述
https://stackoverflow.com/a/26703445/783043
,余弦相似性对零向量没有多大意义,导致Elasticsearch提出了关于它们的错误。
这个问题的解决方案是切换到不同的相似性度量,或者如果您不打算搜索字段,则避免对其进行索引。
解决方案1:
DELETE test
PUT test
{
"mappings": {
"properties": {
"vector": {
"type": "dense_vector",
"dims": 3,
"similarity": "l2_norm"
}
}
}
}
POST test/_bulk?refresh=true
{ "index": { "_id": "1" } }
{ "vector": [1, 5, -20]}
{ "index": { "_id": "2" } }
{ "vector": [0, 0, 0]}
解决方案2:
DELETE test
PUT test
{
"mappings": {
"properties": {
"vector": {
"type": "dense_vector",
"dims": 3,
"index": false
}
}
}
}
POST test/_bulk?refresh=true
{ "index": { "_id": "1" } }
{ "vector": [1, 5, -20]}
{ "index": { "_id": "2" } }
{ "vector": [0, 0, 0]}