gpt4 book ai didi

python - Elasticsearch脚本使用densed_vector查询余弦相似度给出 “class_cast_exception”错误

转载 作者:行者123 更新时间:2023-12-03 01:12:51 76 4
gpt4 key购买 nike

执行此查询时,我使用的是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维 vector 。
注意: \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)")

最佳答案

如果可能,请检查变量数是否等于映射中的维数,即dims:768“query_vector”中的值数等于768吗?
我建议通过运行以下命令再次检查映射,以查看映射是否良好:

GET index_name/_mapping
另外,您可能在传递“query_vector”时错过了一个值。
我在本地对 vector 进行了3维测试。
title_embed的映射为3,类型为“dense_vector”。
我在映射中提取了一些数据,如下所示:
POST /index_name/_doc
{
"title_embed": [10.01,15,15]
}
我如上所述尝试用较低的 vector 尺寸复制您的查询:
{
"size": 10,
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "cosineSimilarity(params.query_vector,'title_embed') + 1.0",
"params": {
"query_vector": [-0.19277021288871765, 0.10494251549243927,12.202022]

}
}
}
}
}
注意:如Tom Elias所述,运行doc ['title_embed']可以运行,但在7.9.0版中已弃用。
一个小建议是,如果可以在将索引和映射一起提取到索引中的数据时通过减小 vector 维来尝试使用较小的维。如果维数为5,则在将数据也提取到索引和“query_vector”中时,检查映射中的“dim”值是否为5。
"query_vector": [12,-1020.02000,10,-5.0000,2]
如果这不起作用,我认为可能对允许的尺寸数量有内部限制。
有用的网址:
https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl-script-score-query.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/dense-vector.html

关于python - Elasticsearch脚本使用densed_vector查询余弦相似度给出 “class_cast_exception”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63678838/

76 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com