У меня получилось уместить это в скрипте:
curl -X GET "localhost:9200/index/doc/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size" : 3,
"query": {
"function_score": {
"query": {
"match_all": {}
},
"script_score" : {
"script" : {
"lang": "painless",
"source": "String forSearch = params.my_modifier; String haveInBase = doc['"'brandName'"'].value; Set nx = new HashSet(); Set ny = new HashSet(); for (int i=0; i < forSearch.length() - 1; i++) { char x1 = forSearch.charAt(i); char x2 = forSearch.charAt(i+1); String tmp = \"\" + x1 + x2; nx.add(tmp); } for (int j=0; j < haveInBase.length()-1; j++) { char y1 = haveInBase.charAt(j); char y2 = haveInBase.charAt(j+1); String tmp = \"\" + y1 + y2; ny.add(tmp); } Set intersection = new HashSet(nx); intersection.retainAll(ny); double totcombigrams = intersection.size(); return (2*totcombigrams) / (nx.size()+ny.size())",
"params": {
"my_modifier": "Невские берега"
}
}
}
}
}
}
'
# То что выше, это однострочный:
String forSearch = params.my_modifier;
String haveInBase = doc['"'brandName'"'].value;
Set nx = new HashSet();
Set ny = new HashSet();
for (int i=0; i < forSearch.length() - 1; i++) {
char x1 = forSearch.charAt(i);
char x2 = forSearch.charAt(i+1);
String tmp = "" + x1 + x2;
nx.add(tmp);
}
for (int j=0; j < haveInBase.length()-1; j++) {
char y1 = haveInBase.charAt(j);
char y2 = haveInBase.charAt(j+1);
String tmp = "" + y1 + y2;
ny.add(tmp);
}
Set intersection = new HashSet(nx);
intersection.retainAll(ny);
double totcombigrams = intersection.size();
return (2*totcombigrams) / (nx.size()+ny.size());
Вот теперь думаю, смогу ли я поместить этот скрипт в "Similarity module", чтобы не слать весь скрипт при каждом запросе.
И будут ли у меня проблемы с производительностью.