I am trying to use cosine similarity in script_score function. The query is breaking when the dense vector field is missing in the document against which I am trying to measure similarity is missing.
I spent a lot of time searching how to check if the field is present in document or not, but couldn't succeed.
I tried:
- checking with doc['field_name'] == null
- checking with doc['field_name'].size() == 0
- checking with doc['field_name'].value == null
Query i am using is
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.queryVector, '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"
}
}
}
i am getting following error
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "No field found for [dense_vector_field] in mapping with types []"
}
}
can someone please help me