I am using a script in sort context to achieve custom sorting. The following is my script sort node that allows custom sorting.
{
"_script": {
"type": "string",
"script": {
"source": """
def sortingValue = "";
for(query in params.queries) {
if (doc['firstName.keyword'].size() > 0 && doc['firstName.keyword'].value.startsWith(query)) {
sortingValue = '1' +":"+ doc['fullName.keyword'].value;
break;
} else if (doc['lastName.keyword'].size() > 0 && doc['lastName.keyword'].value.startsWith(query)) {
sortingValue = '2' +":"+ doc['fullName.keyword'].value;
break;
} else if (doc['companyName.keyword'].size() > 0 && doc['companyName.keyword'].value.contains(query)) {
sortingValue = '3' + ":" + doc['companyName.keyword'].value;
break;
} else {
sortingValue = '4';
}
}
return sortingValue;
""",
"params": {
"queries": [
"mo",
"buga"
]
}
},
"order": "asc"
}
}
With the script i categorise the result sorted in following way
Search Text: Mo Buga
Category 1: Sort contacts with first name starting with Mo or Buga at the top
Category 2: Sort contacts with last name starting with Mo or Buga at the second category
Category 3: Sort contacts with company name starting with Mo or Buga at the third category
Category 4: Every other matching result in category four
How do i ensure there are no unwanted recompilations.