执行此查询时,我使用的是 Elasticsearch 版本 7.9.0:
curl -XGET 'https:somehost:9200/index_name/_search' -H 'Content-Type: application/json' -d '{
"size": 10,
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "cosineSimilarity(params.query_vector, \u0027title_embed\u0027) + 1.0",
"params": {
"query_vector": [-0.19277021288871765, 0.10494251549243927,.......]}
}
}
}
}'
注意:query_vector是 Bert 生成的 768 维向量。注意:\u0027是单引号的 Unicode。
我收到此错误作为回应:
"cosineSimilarity(params.query_vector, 'title_embed') + 1.0","
^---- HERE"],"script":"cosineSimilarity(params.query_vector, 'title_embed') +
1.0","lang":"painless","position":{"offset":38,"start":0,"end":58},"caused_by":
{"type":"class_cast_exception","reason":"class
org.elasticsearch.index.fielddata.ScriptDocValues$Doubles cannot be cast to class
org.elasticsearch.xpack.vectors.query.VectorScriptDocValues$DenseVectorScriptDocValues
(org.elasticsearch.index.fielddata.ScriptDocValues$Doubles is in unnamed module of loader 'app';
org.elasticsearch.xpack.vectors.query.VectorScriptDocValues$DenseVectorScriptDocValues is in
unnamed module of loader java.net.FactoryURLClassLoader @715fb77)"}}}]},"status":400}
虽然title_embed索引映射中的数据类型是Elasticsearch的dense_vector类型,但是报错说是double不知道为什么?
这是映射:
"mappings": {
"properties": {
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"domain": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"link": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"pub_date": {
"type": "date"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title_embed": {
"type": "dense_vector",
"dims": 768
},
"description_embed": {
"type": "dense_vector",
"dims": 768
}
}
}
当我尝试使用 python 执行此查询时,我收到相同的错误:
status_code, error_message, additional_info
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', "class_cast_exception: class org.elasticsearch.index.fielddata.ScriptDocValues$Doubles cannot be cast to class org.elasticsearch.xpack.vectors.query.VectorScriptDocValues$DenseVectorScriptDocValues (org.elasticsearch.index.fielddata.ScriptDocValues$Doubles is in unnamed module of loader 'app'; org.elasticsearch.xpack.vectors.query.VectorScriptDocValues$DenseVectorScriptDocValues is in unnamed module of loader java.net.FactoryURLClassLoader @6d91790b)")