How does learning to rank work?
- Authors
- Name
- Amit Shekhar
- Published on
Learning to rank is a way of using machine learning to teach a computer the best order for search results, by learning from real examples instead of hand-set rules.
In this blog, we will learn about how learning to rank works. We will also see why ordering search results is hard, how a computer learns the best order from examples, and the main approaches used in real search systems.
We will cover the following:
- What is ranking?
- Why simple sorting is not enough
- What is learning to rank?
- The training data we need
- Features: how we describe each result
- The three main approaches
- A simple step-by-step example
- How we measure if the ranking is good
- Where learning to rank is used
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning at Outcome School.
Let's get started.
What is ranking?
Ranking means putting things in order, from best to worst, for a given question.
Suppose we open a search box and type "best pizza near me". The search engine has thousands of results that could match. But it cannot show all of them at the top. It must decide which result comes first, which comes second, which comes third, and so on.
That ordering is ranking. Ranking answers one question: "Out of all these options, which one should we show first?"
Now, this sounds easy when there are only three options. But real systems deal with millions of options. And the order matters a lot. Most people only look at the first few results. If the best answer is at position fifty, almost nobody will see it.
Why simple sorting is not enough
Let's try the simplest idea first.
When we type a search, the search engine could just count how many times our words appear on each page. The page with the most matches goes to the top. The page with fewer matches goes lower.
This is sorting by a single number, which is the word count.
A page can repeat the word "pizza" a hundred times and still be a useless page. Maybe it is spam. Maybe it is old. Maybe it is in the wrong city. Word count alone does not tell us if the page is actually helpful. We have a detailed blog on how BM25 works that explains how search engines score pages by their words in a smarter way.
So, we try a second idea. We add more rules by hand. For example:
- If the page has our word in the title, push it up.
- If the page is popular, push it up.
- If the page is recent, push it up a little.
- If the page is far away from us, push it down.
Now we have many signals, not just one. But we have a new problem.
How much should each signal count? Should a recent page beat a popular page? Should the title match beat the distance? We do not know the exact mix.
A human could guess these numbers. But there are hundreds of signals, and the right mix changes for different kinds of searches. Guessing by hand becomes impossible.
So, here comes learning to rank to the rescue.
What is learning to rank?
Learning to rank means we let the computer learn, from real examples, how to put results in the best order. Instead of a human guessing how much each signal counts, the computer figures out the right mix on its own.
So, learning to rank is machine learning applied to one specific job, which is ordering results.
Earlier, a human said "the title match is worth 5 points and popularity is worth 3 points". Now, the computer looks at thousands of past examples and decides these point values by itself, in a way that gives the best order.
The training data we need
To learn anything, the computer needs examples. These examples are called training data, which means the past examples we use to teach the computer.
For ranking, one example is not just one result. One example is a full set of results for a single search.
Let's make this concrete.
Suppose someone searched for "running shoes". For that one search, we have a list of results. And for each result, we have a label that tells us how good it was.
A label is just a score that says how relevant a result was for that search. For example, we can use a simple scale like below:
0means not relevant at all1means somewhat relevant2means very relevant
So our training example for "running shoes" could look like below:
Search: "running shoes"
Result A -> label 2 (very relevant)
Result B -> label 0 (not relevant)
Result C -> label 1 (somewhat relevant)
Here, we can see that for one search, we have three results with three labels. The computer's goal is to learn an order that puts the high labels at the top. So for this example, the perfect order is A, then C, then B.
Now, the natural question is: where do these labels come from?
There are two common sources.
First, human raters. Real people look at a search and its results and give a relevance score. This is slow and costly, but very accurate.
Second, user behavior. When people search and then click a result, that click is a hint that the result was useful. If many people skip the first result and click the third one, that tells us the third one was better. We can turn these clicks into labels.
So, we collect many such searches, each with its results and labels. This is our training data.
Planning your move into AI?
No matter which tech domain you work in today, with your effort, you can transition to these roles:
- AI Engineer
- Forward Deployed Engineer
- AI Agent Engineer
- Harness Engineer
- Voice AI Engineer
- AI Architect
- LLM Engineer
- LLM Inference Engineer
- Gen AI Engineer
- MLOps Engineer
- ML Engineer
- Deep Learning Engineer
Our AI and Machine Learning Program at Outcome School takes you there.
Features: how we describe each result
The computer cannot read a web page the way we do. It needs numbers. So we describe each result using a list of numbers called features.
A feature is a single measurable fact about a result. Each feature captures one signal we think matters.
Let's look at some example features for a search and a result:
- Does the search word appear in the title? (
1for yes,0for no) - How many times does the search word appear on the page?
- How popular is the page, measured by how many other pages link to it?
- How recent is the page, measured in days?
- How far is the place from the user, measured in kilometers?
So, every result becomes a short list of numbers, like below:
Result A -> [title match: 1, count: 8, popularity: 90, age: 5, distance: 2]
Result B -> [title match: 0, count: 2, popularity: 10, age: 400, distance: 30]
Here, we can see that each result is now just a row of numbers. The computer is good at finding patterns in numbers. So once we turn results into features, the computer can start learning.
This step is very important. The quality of the features decides the quality of the ranking. If we feed in weak signals, the ranking will be weak.
The three main approaches
Now we have training data, labels, and features. The next question is: how exactly does the computer learn the order?
There are three main approaches. We will understand each one with a simple idea.
Approach 1: Pointwise.
In the pointwise approach, the computer looks at one result at a time. It tries to predict the label for that single result, like a score from 0 to 2.
It is like a teacher grading each answer sheet on its own, without comparing students. Once every result has a predicted score, we sort the results by that score, from high to low.
The advantage is that it is simple. The disadvantage is that ranking is really about order, not about exact scores. Predicting the exact number for each result is harder than we need, and it can miss the bigger picture of the full list.
Approach 2: Pairwise.
In the pairwise approach, the computer looks at two results at a time. It does not try to predict an exact score. Instead, it answers a simpler question: "Between these two results, which one should come first?"
It is like a teacher who does not give exact marks, but can always say "this answer is better than that one". We take many such pairs and teach the computer to get each comparison right.
This matches ranking much better, because ranking is about which one beats which one. The pairwise approach is very popular and works well in practice.
Approach 3: Listwise.
In the listwise approach, the computer looks at the whole list of results for a search at once. It tries to find the order that makes the entire list as good as possible.
It is like a teacher who arranges all the answer sheets in the best order in one go, instead of comparing them two by two. This is the most direct match to what we actually want, which is the best full ordering. It is also the most advanced of the three.
Let me tabulate the differences between the three approaches for your better understanding.
| Approach | What it looks at | Question it answers |
|---|---|---|
| Pointwise | One result | What is the score of this result? |
| Pairwise | Two results | Which of these two comes first? |
| Listwise | The whole list | What is the best order for all of them? |
Here, we can see that as we move down the table, the approach gets closer to the real goal, which is the best order for the full list.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
A simple step-by-step example
Let's walk through a tiny example to see the full flow.
Suppose a user searches for "coffee shop". We have three results, A, B, and C. We will use the pointwise approach because it is the easiest to picture.
Step 1: We turn each result into features. We measure the signals and get numbers for each result, like below:
A -> [title match: 1, popularity: 80, distance: 1]
B -> [title match: 1, popularity: 40, distance: 10]
C -> [title match: 0, popularity: 90, distance: 2]
Step 2: The model gives each result a score. The model has already learned, from past data, how much each feature counts. Let's say it learned that title match and being close are very valuable, and popularity helps a little. It now produces a score for each result, like below:
A -> score 9.0
B -> score 6.0
C -> score 5.5
Step 3: We sort by the score. We put the highest score first. So the final order is A, then B, then C.
Let's see the whole flow as below:
+--------------------------------+
| Raw results: A, B, C |
+--------------------------------+
↓
+--------------------------------+
| Features: numbers for each |
+--------------------------------+
↓
+--------------------------------+
| Learned model: A 9.0, B 6.0, |
| C 5.5 |
+--------------------------------+
↓
+--------------------------------+
| Sort by score (high to low) |
+--------------------------------+
↓
+--------------------------------+
| Final order: A, B, C |
+--------------------------------+
Here, we can see the whole idea in action. We started with raw results, turned them into numbers, let the learned model score them, and sorted by the score. The result is a ranked list.
Now, one important point. How did the model "learn" how much each feature counts? During training, it saw many past searches with labels. Each time its order was wrong, it adjusted the weight of each feature a little, so the next time the order would be closer to correct. After many rounds, the weights settle into a good mix. We have a detailed blog on how gradient descent works that explains this step by step.
Here is the training loop as below:
+-----------------------------------------------+
| |
↓ |
Show the model a past search with labels |
| |
↓ |
Model produces an order |
| |
↓ |
Compare with the correct order |
| |
↓ |
Wrong? Adjust the feature weights a little ------+
|
↓
Right enough? Stop. The weights are now good.
Here, we can see that the model keeps looping. Each round, it fixes its mistakes a little, until the order is good enough.
This is how the model learns the right balance without any human guessing the numbers.
To learn Supervised Learning, Gradient Descent, and Loss Functions in depth, check out our AI and Machine Learning Program at Outcome School.
How we measure if the ranking is good
We have a ranked list. But how do we know it is actually good? We need a way to measure quality.
The key idea is simple. A good ranking puts the most relevant results near the top. So our measurement must reward results that are both relevant and high up.
Let's understand two common ideas.
First, top results matter most. A correct result at position one is worth much more than a correct result at position ten. So good measures give more weight to the top positions and less weight to the lower ones. A measure called NDCG does exactly this. It checks how good our order is, while giving more credit for getting the top spots right. The closer the score is to 1, the closer our order is to perfect.
Second, did we find the good ones early. Another idea is to check, as we go down the list, how quickly we hit the relevant results. If the relevant results show up early, the score is high. If they show up late, the score is low.
A higher score means a better order. So, we use these scores to compare one ranking model with another, and many training methods are designed to push these scores up.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Where learning to rank is used
Now that we understand how it works, let's see where it shows up in our daily life.
- Web search. When we type a query, search engines often use learning to rank to order the millions of matching pages.
- Online shopping. When we search for a product, the store orders the items to show the ones we are most likely to buy.
- Streaming and video. The list of movies or videos suggested to us is an ordered list, ranked for our taste.
- Job and travel sites. Job listings, flights, and hotels are all ordered using these ideas.
- Maps and local search. When we look for a restaurant or a store, the nearby and good options are pushed to the top.
In all of these, we describe each option with features, we learn from past examples how to order them, and we sort to show the best ones first.
Frequently Asked Questions
Which learning to rank approach is the most popular?
The pairwise approach is very popular and works well in practice. It looks at two results at a time and learns which one should come first, which matches ranking well because ranking is about which result beats which. The listwise approach is the most direct match to the goal of the best full ordering, and it is also the most advanced of the three.
Can user clicks be used as labels for learning to rank?
Yes. When people search and then click a result, that click is a hint that the result was useful. If many people skip the first result and click the third one, that tells us the third one was better. We can turn these clicks into labels, alongside labels from human raters, which are slow and costly but very accurate.
What is NDCG in ranking?
NDCG is a measure that checks how good a ranked order is, while giving more credit for getting the top spots right. A correct result at position one is worth much more than a correct result at position ten. The closer the NDCG score is to 1, the closer our order is to perfect.
Do features matter in learning to rank?
Yes. The quality of the features decides the quality of the ranking. Each feature is a single measurable fact about a result, like a title match, popularity, or distance. The computer learns patterns from these numbers, so if we feed in weak signals, the ranking will be weak.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
