0

执行此查询时,我使用的是 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)")
4

1 回答 1

1

如果可能,检查变量的数量是否等于映射中的维数,即

dims:768

“query_vector”中的值的数量是否等于 768?

我建议通过运行以下命令再次检查映射以查看映射是否良好:

GET index_name/_mapping

此外,您可能在传递“query_vector”时错过了一个值。

然而,我在本地做了一个测试,向量的 3 个维度。

title_embed 的映射为 3,类型为“dense_vector”。

我在我的映射中摄取了一些数据,如下所示:

POST /index_name/_doc
{
  "title_embed": [10.01,15,15]
}

如上所述,我尝试使用较低的向量维度复制您的查询:

{
"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 版本中已弃用。

一个小建议是,在索引中与映射一起摄取数据时,是否可以通过减少向量维度来尝试使用较低维度。如果维数为 5,则检查映射中的“dim”值是否为 5,同时将数据摄取到您的索引和“query_vector”中

"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

于 2020-09-03T10:49:00.740 回答