ElasticSearch:如果文档中存在字段,则在 script_score 函数内执行余弦相似度

ElasticSearch: Execute Cosine Similarity inside script_score function if field is present in the document

我正在尝试在 script_score 函数中使用余弦相似度。当我试图测量相似性的文档中缺少密集向量场时,查询中断。

我花了很多时间搜索如何检查文档中是否存在该字段,但没有成功。

我试过了:

  1. 检查文档['field_name'] == null
  2. 检查文档['field_name'].size() == 0
  3. 检查文档['field_name'].value == null

我正在使用的查询是

POST /sidx-4111c0fc-a8ba-523c-9851-34a2b803643b/_search/
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": {
            "multi_match": {
              "query": "nri customer bank loan",
              "fields": [],
              "fuzziness": "AUTO"
            }
          }
        }
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "double score =0; score = doc['dense_vector_field'] == null ?0: cosineSimilarity(params.qv, 'dense_vector_field'); if(score>=0.8 && score<=1.0){return 10000+score;} else if(score>=0.60 && score<0.80){return score+1000;} else{return score+100}",
              "params": {
                "qv": [1,1,0,1]
              }
            }
          }
        }
      ],
      "boost_mode": "sum"
    }
  }
}

我收到以下错误

  "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "No field found for [dense_vector_field] in mapping with types []"
          }
        }

谁能帮帮我

您也可以将 doc.containsKey('dense_vector_field') 称为布尔值 returns。

在相关说明中,当参数中的唯一键是 qv 时,为什么要访问 params.queryVector