knn_filter
The function knn_filter(str vector_fieldname1, str vector_fieldname2, float min_score)
allows to filter candidates based on vector distance (KNN) between document fields, according to the distance metric defined in the data schema config file.
if the distance score is below min_score, the function will return 0. Otherwise, it will return 1.
Any arithmetic combination (+, *, *, /) of knn_filter()
and a variable or a constant is allowed.
min_score
can be a dynamic value, included in the query parameters.
Input
vector_fieldname1 (str) - the name of the query field to use in the KNN calculation. params[fieldname1] must be of type dense_vector.
vector_fieldname2 (str, default=fieldname1) - the name of the document field to use in the KNN calculation. params[fieldname1] must be of type dense_vector. By default vector_fieldname2 is set to vector_fieldname1.
min_score (float, default=0) - the score threshold. If the distance score is below this value, distance() will return 0.
Output
result (float) - The returned value is 1.0 If the KNN distance between vector_fieldname1 to vector_fieldname2 is greater the min_thershold, and 0.0 otherwise.
Example
Limitations
params[vector_fieldname2] and doc[vector_fieldname2] must be indexed using the same metric.
The knn_filter function can only be used as part of a return statement. In addition, all other
return
statements must onlyreturn 0
,False
or none. For example, only return statements of the following types are allowed:
The knn_filter() function allows to you to filter based on the KNN score, as part of Hybrid Search Score functions.
Last updated