Last active
December 12, 2018 10:22
-
-
Save d-demirci/b4e402189934bc840a10370c44c50982 to your computer and use it in GitHub Desktop.
painless script to sort by calculating Euclidean distance in elastic search 6.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//can be used with -> curl -XGET -H "Content-Type:application/json" http://es_host:es_port/image_index/_search -d@euclidean_distance_sort.json | |
{ | |
"sort" : { | |
"_score" : "asc" | |
}, | |
"query": { | |
"function_score" : { | |
"script_score" : { | |
"script" : { | |
"lang" : "painless", | |
"source":" int distance = 0;int max_score = 9363600 ; def docvalues = params._source['vec']; if(docvalues.length != params.query_feature.size()){ distance = max_score; }else{ int i=0;for(val in docvalues){int diff = (int)val - (int)(params.query_feature[i]); diff=(int)(Math.abs(diff)); distance = diff*diff; i += 1;}}return Math.sqrt(distance);" | |
, | |
"params" : { | |
"query_feature" : [ 555,1125,1116, 728, 292, ..omitted] | |
} | |
} | |
} | |
} | |
}, | |
"size":10, | |
"from":"0" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment