在无痛弹性搜索中使用自定义 Dotproduct 时得到不准确的结果。
索引映射。
PUT /custom_dot_product
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"level1": {
"type": "nested",
"properties": {
"level2": {
"properties": {
"vector_3": {
"type": "long"
}
}
}
}
}
}
}
}
文档数据 .
POST /custom_dot_product/_doc/1
{
"level1": {
"level2": {
"vector_3": [
2,
2,
2,
0
]
}
}
}
搜索查询
POST custom_dot_product/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "level1",
"query": {
"function_score": {
"query": {
"match_all": {}
},
"script_score": {
"script": {
"source": " if(doc['level1.level2.vector_3'].size()>0){ double result = 0.0; for (int i = 0; i < params.user_vector.length; i++) result += params.user_vector[i] * doc['level1.level2.vector_3'][i]; return result;} ",
"params": {
"user_vector": [
1,
1,
1,
0
]
}
}
},
"boost_mode": "replace"
}
},
"inner_hits": {
"sort": []
}
}
}
]
}
}
}
输出:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 4.0,
"hits" : [
{
"_index" : "knn_dot_product_poc",
"_type" : "_doc",
"_id" : "1",
"_score" : 4.0,
"_source" : {
"level1" : {
"level2" : {
"vector_3" : [
2,
2,
2,
0
]
}
}
},
"inner_hits" : {
"level1" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 4.0,
"hits" : [
{
"_index" : "knn_dot_product_poc",
"_type" : "_doc",
"_id" : "1",
"_nested" : {
"field" : "level1",
"offset" : 0
},
"_score" : 4.0,
"_source" : {
"level2" : {
"vector_3" : [
2,
2,
2,
0
]
}
}
}
]
}
}
}
}
]
}
}
score 结果应该是 6.0,但它给出了 4.0 当我从向量中删除 0 时,它开始给出 6.0
剧本评分后面还有其他计算吗?