Hi everyone. I have business needs to create multiple fields ranking sorting. What does it mean? It means that I want to be able to sort by several fields with custom boost and different order.
test dataset :
interface Car {
price: number,
age: number,
engineDisplacement: number,
}
To achieve it I see 3 steps:
- Consider one attribute at a time and try to maximize or minimize it (as per the requirement) to generate optimized score.
- Introduce weights to each attributes to get optimized weighted scores.
- Combine the weighted scores (of each attribute) to create a final score for an entity.
For 1 step I need to calculate maximization and minimization.
Let's A would be an attribute that we want to maximize, and it's elements are: [a1, a2, ..., aN]; 1< i < N
Maximization I can get with next approaches:
1) maximization = a[i]/sum(A);
2) maximization = a[i]/max(A);
3) maximization = (a[i] - min(A))/(max(A) - min(A));
Minimization I can get with next approaches:
1) minimization = 1/maximization(a[i], A);
2) minimization = 1 - maximization(a[i], A);
After calculation maximization and minimization we can apply weight to each attribute. If attribute has "asc" order - we apply minimization, if "desc" - maximization. We want to apply sorting by price(asc) with weight 60, age(asc) with weight 20 and engine displacement (desc) with weight 20;
score = minimization(price)*60 + minimization(age)*20 + maximization(engine displacement)*20
The question is can I somehow to implement it?
I tried multiple field sorting in elastic:
"sort": [
{ "price": "asc" },
{ "age": "asc" },
{ "engineDisplacement": "desc" }
]
But I don't see ability to apply weight.
Also I tried script_score to sort by my self, but I didn't find way to get max(A) and min(A).