# Multiple fields ranking sorting

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:

1. Consider one attribute at a time and try to maximize or minimize it (as per the requirement) to generate optimized score.
2. Introduce weights to each attributes to get optimized weighted scores.
3. 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).

I found only one way to achieve my goal: make 2 requests.
From first request I get aggregations for fields max values. And in second request I do my sorting in the script.
If someone knows how to do it better or how to do it in one request, please share your idea.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.