Learning to Rank in Elasticsearch
How a search engine learns from real people what a good result looks like, and how Elasticsearch uses a trained model to put the best answers at the very top.
Imagine you walk into a huge shop and ask the person at the desk for running shoes. The shop has forty thousand pairs. If the assistant simply hands you the first pair they happen to find, you will probably leave unhappy. A good assistant does something smarter. They think about which shoes are popular, which ones fit most people, which ones are in stock, and which ones match what you seem to want. Then they place the best few pairs in your hands first.
A search engine faces this exact problem thousands of times every second. Finding results that match a query is the easy part. Deciding which result goes first is the hard part. Most people never scroll past the first few results, so the order decides whether a search feels clever or useless. A great result buried at position forty might as well not exist.
Learning to Rank, usually shortened to LTR, is a way to teach a computer this ordering skill by showing it real examples. This guide explains LTR in Elasticsearch from zero. Every new word is explained when it first appears, and each idea builds on the one before it. By the end you will understand what LTR is, how its parts fit together, how a ranking model is built and checked, what it costs, and where the common traps are hiding.
1What Is Learning to Rank?
Before any Elasticsearch details, we need a clear picture of the problem LTR solves and the simple idea behind the solution.
Search Is Really Two Jobs
A search engine is a program that stores a large collection of items and finds the ones that match what a person typed. Elasticsearch is one of the most widely used search engines in the world. It stores items called documents (a product, a news article, a job post, a support page) and can find matching documents among millions in a few milliseconds.
Every search does two jobs. The first job is retrieval: finding every document that could reasonably answer the query. The second job is ranking: putting those documents in the best possible order. Elasticsearch has always been very good at retrieval. Ranking is where Learning to Rank steps in.
Scores Decide the Order
When Elasticsearch finds matching documents, it gives each one a number called a relevance score. A higher score means a better match, and the results are sorted from the highest score to the lowest. By default this score comes from a formula that mostly counts how well the words in the query appear in the document. We will look at that formula in the next chapter.
The important point is that a score is just a number, and somebody has to decide how that number is calculated. Traditionally, a human engineer writes the rules. Learning to Rank changes who writes the rules. Instead of an engineer guessing, a machine-learning model studies thousands of past searches and learns which combinations of signals lead to results that people actually liked.
Think of a librarian who has worked at the same desk for twenty years. Nobody gave this librarian a formula for ordering book suggestions. Over time they noticed patterns: students want the newest edition, people asking about cooking usually prefer books with photos, and a book that keeps getting borrowed is usually a good one. Learning to Rank is a way to give a search engine the same experience. The model watches many visitors, remembers what worked, and turns those patterns into an ordering skill.
The Plain Definition
Learning to Rank (LTR) is a technique where a machine-learning model is trained on examples of good and bad search results, and is then used to reorder the results of new searches so that the most useful ones appear first.
In Elasticsearch, LTR is a built-in feature. You train a ranking model outside the cluster, upload it into Elasticsearch, and then ask Elasticsearch to use that model to reorder the best candidates found by a normal first search. The reordering step is called rescoring, and the piece of Elasticsearch that does it is called a rescorer.
The Three Ingredients
Every LTR system, in Elasticsearch or anywhere else, is made of the same three ingredients. Keep these in your head, because the rest of this guide expands each one.
- Features: measurable facts about a query and a document, such as how well the title matches, how many stars the product has, or how recently the article was published.
- Judgments: examples that tell the model which documents were good answers for which queries, gathered from human reviewers or from user behavior.
- A model: the learned function that takes the features of a document and produces a score that decides its position.
What LTR Is Not
LTR does not find documents. If a good document is missing from the first-stage results, no ranking model can bring it back. LTR also does not train itself inside Elasticsearch. The training happens in a separate environment, usually written in Python, and Elasticsearch only runs the finished model. Finally, LTR is not magic. A ranking model can only be as good as the features it sees and the judgments it learned from.
2Why Plain Relevance Scoring Is Not Enough
To see why LTR exists, we first look at how Elasticsearch normally ranks results and where that approach runs out of road.
How the Default Score Works
By default, Elasticsearch scores text matches with a formula called BM25. The name is not important, but the idea is. BM25 looks at three simple things. First, how many times does the query word appear in the document? More appearances usually means a better match. Second, how rare is the word across the whole collection? A rare word like “espresso” tells you more than a common word like “the”. Third, how long is the field? A word appearing once in a short title matters more than the same word appearing once in a huge page.
BM25 is fast, simple and surprisingly strong. For many small projects it is all you need. It has powered search on countless websites for years, and it is still the first stage of most LTR systems.
What BM25 Cannot See
BM25 only understands words. It has no idea what people do after they search. It cannot tell that one product sells ten times more than another, that a news article is three years old, or that a job post was filled last week. It also cannot tell what a word means in context. A person searching for “jaguar” might want a car, an animal, or a football team, and word counting alone cannot know which.
What do other people pick?
A result that thousands of people buy or click is probably better than one nobody touches, but word counting never sees this.
How new is it?
For news, events and job posts, newer usually wins. For a classic reference page, older may be fine. The right balance depends on the query.
How good is it?
Star ratings, return rates, page completeness and seller reputation all say something about quality that text cannot.
What matters to the shop?
Stock levels, delivery speed and margin can all affect which result should come first for a given customer.
Who is asking?
A person on a phone in Mumbai and a person on a laptop in Berlin may want different things from the same words.
What did they intend?
The same word can mean different things. Signals such as the category a person browsed can help pick the right meaning.
The Hand-Tuned Boost Trap
Engineers have long tried to fix these gaps by hand. Elasticsearch lets you add boosts, which are extra points for certain conditions. You might add points for high ratings, more points for recent dates, and a few more for items in stock. Each boost is a number that someone chose by guessing and testing on a handful of queries.
This works at first. Then the trouble starts. Boosts interact with each other in ways that are hard to predict. Making ratings matter more may accidentally push old, low-quality items above fresh ones. Fixing the query “wireless headphones” may quietly break “headphones for running”. After a year, the scoring rules become a tangle that only one engineer understands, and nobody dares to touch them.
Hand-tuning boosts is like adjusting an old radio that has thirty knobs. You turn one knob to remove static from one station, and another station goes silent. You could spend your whole career tuning and never find the perfect setting for every station at once. Learning to Rank replaces the thirty knobs with a machine that tries millions of combinations on its own, using real listener feedback as the guide.
Hand-tuned boosts do not fail loudly. They fail by slowly making some queries worse while others get better. Without a proper measurement setup, teams often cannot even tell whether the last change helped or hurt.
This is the gap LTR fills. It keeps the strengths of BM25 for finding candidates, and it adds a learned layer on top that understands popularity, freshness, quality and context together, with the weights chosen by data instead of guesswork.
3Core Concepts You Need First
A handful of words show up again and again in LTR. Learn them once here and the rest of the guide will read easily.
Query
The words a person types into the search box, plus any extra context such as country, device or selected filters.
Document
One item stored in Elasticsearch, such as a product page or an article. Documents are made of fields like title, price and rating.
Feature
A single number that describes something about a query and document pair. A model reads many features together to decide a score.
Judgment
A grade saying how good a document is for a query, for example 0 for useless up to 4 for perfect. Also called a label.
Judgment List
A big table of queries, documents and grades. It is the answer key the model studies while learning.
Training
The process where the model reads the judgment list and features and adjusts itself until it orders documents like the answer key does.
First-Stage Retrieval
The normal, fast search that finds candidate documents before any learned reordering takes place.
Rescoring Window
The number of top candidates from the first stage that will be passed to the model for reordering.
Why Two Stages?
A ranking model looks at many features for every document it scores. Computing those features and running the model costs real time. Doing that for every document in a collection of ten million would be far too slow. So LTR systems use a funnel. A cheap, fast first stage narrows millions of documents down to a few hundred candidates. An expensive, smart second stage then carefully orders only those candidates.
Hiring works the same way. A company that receives five thousand resumes cannot interview everyone. A quick screening step removes obvious mismatches and leaves fifty people. Then the hiring team spends real time interviewing those fifty and puts them in order. The screening is fast and rough. The interview is slow and careful. Search does exactly this with documents.
flowchart LR
A["Millions of documents in the index"] --> B["First stage: fast search with BM25 or vectors"]
B --> C["Top few hundred candidates"]
C --> D["Second stage: LTR model scores each candidate"]
D --> E["Reordered candidates"]
E --> F["Top 10 shown to the person"]
Read the figure from left to right. The index holds every document. The first stage uses cheap scoring to keep only the most promising ones. The LTR model then reorders those survivors using richer information. Finally the application shows the first ten. If a good document is dropped in the first stage, the model never gets to see it, which is why the first stage must be generous enough to include all the good candidates.
How We Know a Ranking Is Good
To train and test a model we need a way to score a whole list of results. The most popular measure is called NDCG, which stands for Normalized Discounted Cumulative Gain. The name is scary, but the idea is friendly. It rewards a list for putting highly graded documents near the top and gives less credit for good documents placed lower. A perfect ordering scores 1.0, and worse orderings score lower. We will use this measure again when we evaluate models later.
4Three Families of Ranking Models
Not all LTR models learn in the same way. Researchers group them into three families based on what question the model asks while learning.
Picture a talent show with twenty acts. One judge gives each act a score from 1 to 10 without comparing it to anyone else. Another judge watches two acts at a time and only says which of the two was better. A third judge watches the whole line-up and tries to build the best final ranking from first place to last. These three judges are the three LTR families.
| Family | Question It Asks | Strength | Weakness |
|---|---|---|---|
| Pointwise | How good is this one document for this query? | Simple to build and easy to understand | Ignores the order of the list, so it may spend effort on unimportant mistakes |
| Pairwise | Is document A better than document B for this query? | Learns relative order directly | Treats all pairs equally, even those far down the list that nobody sees |
| Listwise | What is the best order for the entire list? | Focuses on what matters most, the top positions | More complex to train and tune |
Where LambdaMART Fits
The model type you will meet most often in LTR work is called LambdaMART. It is a listwise-flavored method that combines two ideas. The first idea is gradient boosted decision trees. The second idea is a special way of measuring mistakes that pays extra attention to errors near the top of the list, because a mistake at position one hurts far more than a mistake at position forty.
A decision tree is a series of yes-or-no questions that ends in a score. For example: is the title match strong? If yes, is the rating above four stars? If yes, give a high score. One small tree is not very smart. Boosting builds hundreds of small trees one after another, and each new tree is trained to fix the mistakes left by the trees before it. Added together, the trees make a strong and flexible model.
Boosting is like a team of proofreaders passing a document down a line. The first proofreader catches the big errors. The second catches what the first one missed. The third catches what is still left. No single proofreader is perfect, but the whole line produces a very clean document.
Elasticsearch runs tree-based ranking models, such as those trained with XGBoost, and newer versions also support LightGBM models. Tree ensembles are popular here because they are fast at query time. Scoring a document means walking down a few hundred small trees, which is just a series of quick comparisons and additions.
Why not use a giant neural network instead? Neural networks can be very powerful, but they are heavier to run and harder to explain. Tree ensembles are a sweet spot for structured signals like ratings, prices and match scores. They train quickly, they run quickly, and their decisions can be inspected. That is why they remain a workhorse for production ranking, including at large companies.
5Architecture and Components
An LTR system has an online side that answers searches in real time and an offline side that trains and improves the model. Both sides matter.
The Parts of the System
API Gateway and Load Balancer
The gateway is the single front door for requests, handling login checks and rate limits. The load balancer spreads requests across several copies of the search service so no copy is overloaded.
Search Service
Your own application code. It takes the person’s query, adds context, builds the Elasticsearch request and formats the answer.
Coordinating Node
The Elasticsearch node that receives the request, forwards it to the shards that hold the data, and merges their answers into one list.
Data Nodes and Shards
An index is split into pieces called shards, spread over data nodes. Each shard runs the first-stage search on its own piece and then applies the rescorer to its own top results.
Rescorer and Trained Model
The rescorer computes the features for each candidate and asks the stored model for a score. The model itself lives inside the cluster as a saved object.
Logging and Training Pipeline
Search and click logs feed a judgment list, which feeds a Python training job. The finished model is uploaded into the cluster with a tool called Eland.
flowchart TD
U["User on browser or mobile app"] --> G["API Gateway"]
G --> LB["Load Balancer"]
LB --> S["Search Service"]
S --> CN["Elasticsearch Coordinating Node"]
CN --> SH1["Shard 1: first-stage search"]
CN --> SH2["Shard 2: first-stage search"]
CN --> SH3["Shard 3: first-stage search"]
SH1 --> R1["LTR rescorer with model"]
SH2 --> R2["LTR rescorer with model"]
SH3 --> R3["LTR rescorer with model"]
R1 --> M["Coordinating Node merges ranked lists"]
R2 --> M
R3 --> M
M --> S
S --> LOG["Event log: queries, results, clicks"]
LOG --> TR["Offline training job"]
TR --> EL["Eland model upload"]
EL --> MS["Trained model stored in cluster"]
MS --> R1
MS --> R2
MS --> R3
Reading the Diagram
Start at the top. A person types a query, and the request travels through the gateway and load balancer to the search service. The search service asks Elasticsearch, where the coordinating node fans the request out to shards. Each shard performs the normal first-stage search on its own slice of the data, then immediately applies the LTR rescorer to its best candidates. The coordinating node collects the reordered lists and merges them into the final answer.
Now look at the lower half. Every search and every click is logged. Later, a training job turns those logs into an improved model. The model is uploaded into the cluster and stored there, and the rescorers on the shards read it from that store. This loop is what makes the system improve over time.
Because rescoring happens on the shards, the model must be available on every node that holds shards for the index. Model size, therefore, affects memory on those nodes and not just on one special machine. Keep this in mind when you plan capacity.
6Internal Working: The Life of One Query
Let us follow a single search from the moment a person presses Enter to the moment results appear, one step at a time.
The request is built
The search service receives the words the person typed and adds context such as country, device type and any chosen filters. It builds a request containing the first-stage query and a rescoring instruction that names the model to use.
The coordinating node fans out
Whichever node receives the request becomes its coordinator. It sends the request to one copy of every shard of the index, so all the data gets searched in parallel.
First-stage retrieval on each shard
Each shard runs the normal search and scores documents with BM25, or with vector similarity, or with a mix. Filters remove documents that must not appear, such as items out of stock or hidden from the person’s region.
Features are computed
For each of the top candidates in the window, the shard calculates every feature the model needs. Some features are found by running small extra queries against the candidate, and others are read directly from stored fields such as rating or price.
The model scores each candidate
The list of feature numbers goes into the model. The model walks through its trees, adds up their answers, and produces one final score for the document.
Candidates are reordered and merged
Each shard sorts its window by the new scores and sends the winners back. The coordinating node merges the lists from all shards and picks the overall best for the requested page.
Results are shown and logged
The coordinator fetches the display fields, such as title and image link, and returns them. The search service shows the page and records what was shown, so later clicks can be matched to positions.
The Window Is Per Shard
One detail surprises many beginners. The rescoring window applies to each shard separately. If an index has five shards and the window is one hundred, then up to five hundred documents are scored by the model across the cluster for a single search. That is why the window is the biggest lever on both quality and speed, and we will return to it in the performance chapters.
Documents that fall outside the window are not touched by the model at all. They keep their first-stage position behind the reordered group. This has a practical meaning: the model can only shuffle the top of the list. If your users often page deeper than the window, the model will not influence those deeper pages the way you expect.
Keep the rescoring window comfortably larger than the deepest page you want the model to control. If people usually stop at page two of ten results each, a window that covers at least the first twenty documents per shard is the bare minimum, and a larger one leaves room for the model to promote hidden gems.
7Features: The Signals the Model Sees
A model can only rank using what it is shown. Choosing good features is often the single most valuable part of an LTR project.
What a Feature Looks Like
A feature is one number that describes a query and document pair. “The BM25 score of the query against the title” is a feature. “The product rating” is a feature. “The number of days since the article was published” is a feature. The model receives a list of such numbers for every candidate and learns which combinations point to a good result.
Think of a doctor reading a medical chart. The chart holds temperature, blood pressure, heart rate and age. No single number decides the diagnosis, but together they tell a story. Features are the chart, and the model is the doctor who has seen thousands of charts before.
How well do the words match?
Separate match scores for title, description and tags. A strong title match usually means much more than a weak body match.
How good is the item itself?
Rating, number of reviews, image count, seller score and profile completeness. These come straight from stored fields.
How recent is it?
Age in days since publishing or last update. The model learns for itself how much age matters for each kind of query.
What do people do with it?
Click rate, purchase rate and save rate over recent weeks, computed by a separate data job and written into the documents.
What kind of search is this?
Number of words in the query, whether it looks like a brand name, or whether it contains a number such as a model code.
Who is asking?
Country, language, device type or a favorite category, passed in with the request when your rules allow using it.
How Features Are Defined in Elasticsearch
In Elasticsearch, you describe the features when you upload the model. Each feature has a name and an instruction for how to compute it. The most common instruction is a small query. Elasticsearch runs that query against a candidate document and uses the resulting score as the feature value. To read a stored field such as rating, you use a query that simply returns the field’s value as its score. If the small query does not match a document, you supply a fallback value so the model never receives a blank.
These small queries can contain placeholders. When the real search arrives, your application fills the placeholders with actual values, such as the person’s query text. This is how one feature definition can serve every search.
Consider a restaurant kitchen. The test kitchen develops a recipe using exact measurements: two grams of salt, exactly this much flour. The restaurant must measure the same way every night. If the test kitchen used grams and the restaurant used handfuls, the dish would taste different even though the recipe is the same. Features work exactly like this. They must be measured the same way when training and when serving.
The biggest silent killer of LTR projects is a mismatch between how features were calculated during training and how they are calculated live. If the training job used a rating scaled from 0 to 5 but the live index stores it from 0 to 100, the model will behave strangely and no error message will warn you. Always use one shared definition for each feature, and check live values against training values before launch.
Choosing Features Wisely
More features is not always better. Every feature that runs a query costs time for every candidate in the window. A model with forty features and a window of five hundred documents per shard does twenty thousand small computations per shard for one search. A good habit is to start with a small set of features that you understand, measure the gain, and add more only when they clearly improve results.
It also helps to include features that are cheap to compute. Reading a stored number is nearly free. Running a complicated text query for every candidate is not. When a costly feature gives only a tiny gain, drop it.
Example: An Online Electronics Store
A shop selling headphones defines eight features: title match score, description match score, brand match, price, star rating, number of reviews, days since listing, and purchase rate over thirty days. When a person searches for “wireless earbuds”, the model learns that title match and purchase rate matter most, that a very low price alone is not a good sign, and that products with very few reviews should not outrank well-reviewed ones just because the title matches perfectly.
8Judgment Lists: Teaching by Example
Features describe documents. Judgments tell the model which documents were actually good. Without judgments there is nothing to learn from.
What a Judgment List Contains
A judgment list is a large table. Each row holds a query, a document, and a grade. A common grading scale runs from 0 to 4. Zero means the document is useless for the query, and four means it is a perfect answer. Grades in between show partial relevance, such as a related but not ideal item.
Think of it as an answer key for an exam. The model takes the exam by ordering documents, checks its answers against the key, and slowly gets better. The bigger and more accurate the key, the better the model can become.
Two Ways to Get Judgments
| Source | How It Works | Strengths | Weaknesses |
|---|---|---|---|
| Explicit | Trained human reviewers look at a query and a result and assign a grade using written guidelines | Clean and consistent, and can cover rare queries on purpose | Slow and costly, and it does not scale to millions of queries |
| Implicit | Grades are inferred from behavior: clicks, add-to-cart, purchases, time spent reading | Huge volume, cheap, and reflects real user wants | Noisy and biased, and silent on results nobody saw |
Most mature teams combine both. Human grades act as a trusted small set that keeps everything honest, and behavior data supplies volume. For example, a purchase can be counted as a grade of four, an add-to-cart as three, a click as one, and an impression with no click as zero. The exact weights are a decision you test and adjust.
The Position Bias Problem
Behavior data has a well-known trap called position bias. People click the first result far more often simply because it is first, not because it is best. If you train on raw clicks, the model learns that whatever the old system put first is good, and it just repeats the old ordering with extra confidence.
Suppose a food street has twenty stalls, and a sign points to the first stall saying “start here”. Most visitors eat at the first stall. Does that prove it has the best food? No. It only proves people follow signs. To find the truly best stall you must correct for the effect of the sign, for example by sometimes swapping stall positions and watching what happens.
Teams handle position bias in several ways. They can weight a click at a low position more heavily than a click at position one, because a click far down the list is a stronger sign of interest. They can run small experiments that shuffle results a little to learn true preferences. They can also use click models, which are statistical methods that estimate the real chance a person liked a result after removing the effect of where it was displayed.
Coverage and Cold Start
Queries follow a long-tail pattern. A small number of popular queries, called the head, account for a large share of traffic. Below them are many medium queries, and then a huge tail of rare ones. Judgments naturally pile up on the head and are thin on the tail. A good judgment list samples across all three groups, so the model does not become an expert only on popular searches.
A brand-new site has no behavior data at all. This is the cold start problem. Common answers are to start with human reviewers grading a few hundred important queries, or to launch with a hand-tuned baseline first and collect behavior data before training. Some teams also use large language models to draft grades, but those grades should always be checked against a sample of human-reviewed ones before being trusted.
Write down your grading guidelines in plain language before anyone starts grading. Two reviewers who follow different unwritten rules will produce grades that contradict each other, and a model trained on contradictions learns confusion.
9Training the Model and Loading It into Elasticsearch
Training happens outside Elasticsearch. The cluster’s job is to store the finished model and run it quickly.
The Offline Learning Loop
flowchart LR
A["Search logs and click events"] --> B["Build judgment list with grades"]
B --> C["Compute feature values per query and document"]
C --> D["Train ranking model in Python"]
D --> E["Check quality on held-out queries"]
E --> F["Upload model to Elasticsearch with Eland"]
F --> G["Run online experiment against current model"]
G --> H["Monitor live metrics"]
H --> A
Gather and clean the data
Collect queries, the results shown and the actions people took. Remove bot traffic, duplicate events and obviously broken records. Turn what remains into graded judgments.
Compute the features
For every query and candidate document in the judgment list, calculate the same feature values that Elasticsearch will calculate live. Many teams ask Elasticsearch itself to produce them, which reduces the risk of mismatch.
Split the data by query
Set aside some queries completely for testing. The split must be by query, not by row, because rows from the same query share information and would leak answers into the test.
Train and tune
Train a boosted tree ranking model, try different settings, and watch the score on the held-out queries. Stop when improvement stalls, since training longer only memorizes noise.
Upload with Eland
Eland is an open-source Python library from Elastic. It packages the trained model together with the list of feature definitions and stores them in the cluster under a model name you choose.
Test online, then roll out
Send a small share of real traffic to the new model, compare it to the old one, and expand only if it wins.
Overfitting: The Student Who Memorized
Overfitting happens when a model memorizes its training examples instead of learning general patterns. Picture a student who memorizes last year’s exam answers word for word. That student scores perfectly on the practice exam and fails the real one. In LTR, an overfit model ranks the training queries beautifully and performs poorly on new queries. Holding out unseen queries for testing is the way to catch it.
Feature Importance and Explanations
After training, tree models can report which features they leaned on most. This is very useful. If the model relies heavily on a feature you expected to be minor, it might mean that feature is leaking information about the answer, or that your training data has a hidden bias. Reading feature importance is like asking the model to explain its habits, and it should always be reviewed before launch.
Versions and Names
Every uploaded model has an identifier. Treat identifiers like software versions. Give each a clear name that includes the date or a release number, keep the previous version in the cluster, and change which one the application uses through configuration rather than code. This makes it easy to roll back in seconds if something goes wrong.
10Serving the Model: Rescoring in Production
Once the model is in the cluster, using it is a matter of adding a rescoring instruction to the search request and choosing the right window.
The Rescoring Instruction
A search request in Elasticsearch has a main query and can also carry a rescore section. Inside that section you name the model, set the window size, and pass any parameters the feature definitions need, such as the person’s query text. Elasticsearch runs the main query first, then applies the model to the top candidates.
Because the model is chosen per request, you can send one group of users to one model and another group to a different model. This is how online experiments are run, and it is why storing several model versions side by side is so handy.
Picking the First Stage
The quality ceiling of your whole system is set by the first stage, since the model can only reorder what it receives. Elasticsearch supports several first-stage strategies, and they can be combined.
- Keyword search with BM25: the classic choice. Fast, precise for exact words, and easy to explain.
- Vector search: finds documents whose meaning is close to the query even when the words differ. Good for natural-language questions.
- Hybrid search: merges keyword and vector results so that each method covers the other’s blind spots. The merged candidate list is then handed to the model.
Choosing the Window Size
These ranges are starting points, not rules. Your best window depends on how good the first stage is, how many features you compute and how much time your users will tolerate. A smaller window is faster but may leave a great document stranded outside it. A larger window gives the model more to work with but costs time on every search. The way to decide is to test a few sizes offline and measure both quality and delay.
Filters and Safety Rules
Some rules must always win, no matter what the model thinks. Products that are out of stock, content that must be hidden in a region, or items removed for policy reasons should be removed by filters in the first stage. The model then never sees them, so it cannot accidentally promote them.
Always design a fallback. If the model is missing, or the rescoring step fails or runs too slowly, the application should be able to repeat the search without rescoring and still return reasonable BM25 results. A slightly worse ordering is far better than an error page.
11Measuring Quality: Is the New Ranking Really Better?
A ranking change that feels better in a demo can be worse in reality. Measurement is how you stay honest.
Offline Metrics
Offline measurement uses your judgment list. You run test queries through the ranking and compare the resulting order with the grades. Several standard measures exist, and each answers a slightly different question.
| Metric | Plain Meaning | Best Used When |
|---|---|---|
| Precision at K | Of the first K results, what share were relevant? | Grades are simply relevant or not, and you care about the first page |
| MRR | How high is the first good result, on average? | People usually want one answer, such as a help article or a known item |
| DCG | Total credit for grades, with less credit for lower positions | Grades have several levels and top positions matter most |
| NDCG | DCG scaled between 0 and 1 so different queries can be compared | The standard choice for graded judgments and model comparison |
NDCG in Simple Terms
Imagine a query with three good documents graded 3, 2 and 0. Ordering them 3, 2, 0 puts the best first, so the list earns full credit. Ordering them 0, 2, 3 puts the worthless one on top and buries the best, so it earns much less credit. NDCG turns that difference into a number. Because each query has a different best possible list, NDCG divides by that best list, and the result always falls between 0 and 1, where 1 is perfect.
Elasticsearch has a built-in ranking evaluation feature, called the ranking evaluation API, that lets you supply queries with rated documents and get metrics such as precision, MRR, DCG and NDCG back. It is a convenient way to compare a hand-tuned query against an LTR-based one without leaving the cluster.
Online Experiments
Offline scores are only a rehearsal. The real test is live traffic. In an A/B test, half of the visitors see the current ranking and half see the new one. You then compare business outcomes such as click rate, purchases, or how quickly people find what they need. Another approach called interleaving mixes results from both models into one list and counts which model’s results people prefer. Interleaving needs less traffic and can reach a conclusion sooner.
Always watch guardrail metrics alongside the main goal. A model might raise clicks while raising the zero-result rate, slowing responses, or hurting rare queries. If any guardrail gets worse beyond an agreed limit, the experiment should stop no matter how good the headline number looks.
12Performance and Scalability
LTR adds work to every search. The good news is that the cost is predictable and you control most of the dials.
Where the Time Goes
The extra time for a search with LTR comes from three sources multiplied together: how many documents are in the window, how many features each document needs, and how big the model is. If you double the window, the work roughly doubles. If you add ten more feature queries, each candidate needs ten more small searches. If the model grows from two hundred trees to a thousand, scoring each document takes longer.
Think of a school exam grader. The time to grade depends on the number of papers, the number of questions on each paper, and how thorough the grader is with each answer. Reducing any one of them saves time.
Window Size
The strongest lever. Cutting the window in half nearly halves the rescoring work, at the risk of missing hidden good candidates.
Number of Features
Remove features with tiny importance. Prefer stored-field features over heavy text queries.
Model Size
Fewer and shallower trees run faster. Often a smaller model loses very little quality.
Shard Count
More shards mean more parallel workers but also more windows scored in total. Balance shard count with window size.
Replicas
Extra copies of each shard let the cluster serve more searches at once, which protects speed during traffic spikes.
Hardware
Faster processors and enough memory for the index and model reduce delay. Rescoring is mostly a processor-bound task.
Think in Percentiles
Average response time hides problems. If most searches take 40 milliseconds but one in a hundred takes two seconds, some real people are having a bad time. Always track the 95th and 99th percentile response times, often written p95 and p99. These show what the slower searches experience, and they are the numbers that a latency budget should be set against.
Load Testing Before Launch
Run a realistic load test before switching real traffic to LTR. Replay real queries, at the traffic level of your busiest hour plus a safety margin, against a cluster that has the model loaded. Compare response times with and without rescoring. This tells you exactly how much extra capacity you need, and it prevents surprises on a big sale day.
Example: Trading Quality for Speed
A marketplace measures that a window of 300 per shard gives the best quality but adds too much delay in its busiest hours. Testing shows that a window of 120 keeps almost all of the quality gain and cuts the added time by more than half. The team ships the smaller window, and later brings back larger windows only for searches where the first stage returns very mixed results.
13Data Flow and the Life of a Ranking Model
So far we followed one search. Now we zoom out and follow the data and the model over weeks and months, from first idea to retirement.
Two Flows Running Side by Side
An LTR system has two flows of information. The first is the serving flow, which we already met. A query comes in, candidates are found, the model reorders them, and results go out. It runs in milliseconds and happens millions of times a day.
The second is the learning flow. It runs slowly, in the background, and turns the traces of all those searches into a better model. Events such as “this person searched for this, saw these ten results, and clicked the third one” are sent to a collection point. From there they are stored, cleaned, joined with document data, turned into judgments, and used for training. Many companies move these events through a streaming platform such as Apache Kafka and store them in a large data warehouse or data lake, where scheduled jobs process them each night or each week.
A busy restaurant has the dinner service and the next-morning review. During service, cooks work fast and never stop to reflect. The next morning, the manager reads the order slips, sees which dishes came back to the kitchen and which sold out, and changes the menu. The serving flow is the dinner rush. The learning flow is the morning review, and the improved menu is the new model.
Stages in a Model’s Life
Baseline
Measure the current ranking and set the target to beat. No model is trained yet.
Data Collection
Log searches, results and actions, and gather human grades for a sample of queries.
Build
Define features, assemble the judgment list, train and tune candidate models offline.
Validate
Test on held-out queries, review feature importance, and check special groups such as rare queries and new items.
Shadow and Experiment
Run the model quietly in the background, then on a small share of real traffic against the current champion.
Launch
Increase traffic gradually while guardrail metrics stay healthy. Keep the old model ready.
Operate
Watch quality, latency and data health. Retrain on a schedule or when metrics fall.
Retire
When a newer model wins, remove old versions after a safe waiting period and archive their details for audits.
flowchart LR
B["Baseline measured"] --> D["Data collected"]
D --> BU["Model built offline"]
BU --> V["Validated on held-out queries"]
V --> S["Shadow scoring and A/B test"]
S --> L["Gradual launch"]
L --> O["Operate and monitor"]
O --> R["Retrain with fresh data"]
R --> V
O --> RT["Retire old version"]
Joining the Pieces Correctly
One quiet step in the learning flow causes many bugs: joining events to the right version of the data. Suppose a person saw a product with a rating of 4.1 last Tuesday, but today the rating is 3.8. If you build features from today’s data, the model learns from a rating the person never saw. The safe habit is to record the feature values as they were at the moment of the search, or to keep dated snapshots of the documents so that you can rebuild the past exactly.
This idea is called point-in-time correctness. It is like judging a chess player’s decision using the board as it stood when they moved, not the board as it looks at the end of the game. Without it, the model sees the future and learns habits that do not work in real time.
Store the feature values used for each displayed result in the search log at serving time. This makes point-in-time correctness automatic, saves rebuilding effort, and doubles as a debugging record when someone asks why a result ranked where it did.
14A Worked Example, Start to Finish
Concepts stick better with a story. Here is a small, made-up bookstore example that walks through everything from features to reordered results.
The Setting
A bookstore keeps its catalog in Elasticsearch. A visitor types “python programming for beginners”. The first-stage BM25 search finds many matches. To keep the example small, we look at just five of the top candidates. All numbers below are invented to illustrate the idea and do not come from a real store.
The Features
The store’s model uses five features: how well the title matches the query, the average star rating, the number of purchases in the last thirty days, the age of the book in years, and whether the book is labeled for beginners. Each feature is a single number.
| Book | Title Match | Rating | Purchases (30 days) | Age (years) | Beginner Label | First-Stage Position |
|---|---|---|---|---|---|---|
| Book A: Advanced Python Internals | High | 4.7 | 90 | 2 | No | 1 |
| Book B: Python Basics for Everyone | High | 4.5 | 640 | 1 | Yes | 2 |
| Book C: Python Programming Reference (old edition) | High | 3.9 | 25 | 11 | No | 3 |
| Book D: Learn to Code Gently | Medium | 4.6 | 410 | 2 | Yes | 4 |
| Book E: Programming Puzzles in Python | Medium | 4.2 | 120 | 4 | No | 5 |
What the First Stage Sees
BM25 looks only at words. Book A has the word “Python” many times in its title and description, so it lands in first place. It is a fine book, but it is written for experts, and the visitor asked for beginner material. BM25 has no way of knowing this. Book C is an old edition with the right words, so it takes third place even though few people buy it and its rating is lower.
What the Model Learned
During training, the model studied thousands of searches like this one. It saw that when the query contains the word “beginners”, books labeled for beginners with many recent purchases were bought far more often than books with a slightly higher word match. It also saw that very old editions were rarely chosen. Inside its trees, these lessons look like simple questions: does the query mention beginners? If yes, is the book labeled for beginners? If yes, are the recent purchases high? Add a strong score.
For this query, the model computes a final score for each candidate. It gives Book B the highest score because it combines a strong title match, a beginner label and many purchases. Book D comes second because it is beginner-friendly, well rated and popular even though its title match is only medium. Book A drops to third, since it is strong on match and rating but does not fit the beginner intent. Book E and Book C follow.
| Book | First-Stage Position | Position After LTR | Why It Moved |
|---|---|---|---|
| Book B: Python Basics for Everyone | 2 | 1 | Beginner label, high match, strong recent purchases |
| Book D: Learn to Code Gently | 4 | 2 | Beginner label and popularity outweigh a medium title match |
| Book A: Advanced Python Internals | 1 | 3 | Not for beginners, and fewer purchases |
| Book E: Programming Puzzles in Python | 5 | 4 | Decent rating but no beginner label |
| Book C: Python Programming Reference (old edition) | 3 | 5 | Low rating, few purchases and a very old edition |
What Happens Behind the Scenes
Notice that the store’s engineers never wrote a rule saying “beginner label matters when the query says beginners.” The model discovered it. Also notice that the same model, given a query such as “python internals deep dive”, would learn the opposite preference and put Book A first. This is the great strength of learned ranking: the meaning of each signal can change depending on the query.
Finally, the store measures the result. On the test queries with human grades, the NDCG rises from 0.71 for the old ordering to 0.78 for the new one. These are again illustrative numbers. In a live experiment, the team then watches purchases from the first page of results. If they rise and the response times stay in budget, the model graduates to full traffic.
The model did not create new candidates. All five books were already in the first-stage results. It only decided how to order them, and it used signals that words alone could never reveal. This is the whole promise of LTR in one small picture.
15Advanced Topics for Mature Systems
Once the basics work, teams meet a second wave of questions. These are the ideas that appear in mature ranking systems.
Cascade Ranking
A cascade uses several rankers in a row, each one heavier and applied to fewer documents. A cheap model with a few features scores a large window, and a stronger model with more features scores only the survivors. It is the same funnel idea repeated. Think of airport security: a quick scan for everyone, then a careful check for a small number of travelers. Cascades save time because expensive features are only computed where they can matter.
Multiple Goals at Once
Relevance is rarely the only goal. A shop also cares about revenue, delivery speed and healthy stock levels. A media site cares about reading time and variety. One way to handle this is to train on judgments that already blend these goals, for example by giving purchases with high margin a higher grade. Another way is to train separate models and blend their scores with weights the business chooses. The key is to be explicit about goals, measure each of them, and watch for one goal quietly harming another.
Personalization
A model can use features about the person, such as the categories they browsed recently or the brands they bought, so that two people typing the same query see different orders. This can help a lot for broad queries such as “shoes”. It also brings care requirements. Use only data that people would reasonably expect you to use, respect their privacy choices, and make sure the model still behaves sensibly for visitors who have no history at all.
New Items and the Cold-Start Trap
Behavior features create a chicken-and-egg problem. A new product has no purchases, so the model ranks it low, so nobody sees it, so it never gets purchases. Teams break the loop in a few ways. They can give new items neutral or slightly optimistic behavior values, or give them a small guaranteed exposure for a period. They can also add features based on content similarity to already-popular items. Without such care, a learned ranker can quietly freeze your catalog into a “rich get richer” pattern.
Imagine a street where people only walk into busy shops, because busy looks safe. A new shop with no customers stays empty, so it stays unpopular. A wise mall manager puts a small sign for the new shop at the entrance for a few weeks. Some visitors try it, the good shops earn real reputations, and the mall stays fresh. Exploration in ranking works the same way.
Diversity
Sometimes the top ten results are all nearly identical, such as ten photos of the same kind of sofa from the same seller. Each is individually relevant, but together they waste the page. Diversity methods reorder results so that the list covers different brands, styles or interpretations. This can be done after the model scores the candidates, as a final adjustment step in the search service.
Unbiased and Counterfactual Learning
Earlier we saw that clicks are biased by position. Research on unbiased learning to rank goes further. It uses statistical methods to estimate what would have happened if a result had been shown in a different position, and trains on those corrected estimates. Some companies also use small random shuffles of low-risk positions to collect cleaner evidence. These techniques take extra care and expertise, but they can free a model from simply copying the old system.
Vector Similarity as a Feature
When the first stage includes vector search, the closeness between the query meaning and the document meaning can itself become a feature. The model then blends this meaning-based signal with popularity, freshness and quality signals. This gives you the best of both worlds: language understanding from the vector side and business awareness from the learned ranker.
Explaining a Ranking
People will eventually ask, “Why is this item first?” With tree models, you can inspect the feature values and trace which questions in the trees the item passed. Keeping logged feature values makes this practical. Good explanations build trust with your own team, help find bugs quickly, and support fairness reviews.
Every advanced technique adds moving parts. Adopt them one at a time, and only when measurements show a real problem to solve. A simple, well-monitored model usually beats a sophisticated one that nobody fully understands.
16High Availability, Reliability and Security
A ranking model is part of your production system. It needs the same care as a database or a payment service.
Keeping Search Available
High availability means the system keeps working even when a part fails. Elasticsearch already helps here through replicas, which are extra copies of each shard on different nodes. If a node dies, another copy answers. For LTR, the extra concern is that the model must be present wherever the shards are. When you add or replace nodes, confirm that the search works with the model on the new nodes before sending them full traffic.
Plan for three failure cases. In the first, the model is missing or damaged, so the search service should fall back to normal scoring. In the second, rescoring becomes slow under heavy load, so a timeout should protect the user’s experience. In the third, a new model has bad behavior that was not caught in testing, so you must be able to roll back to the previous version in minutes.
A hospital has a backup generator. Nobody hopes to use it, but everyone is glad it exists when the power goes out. A fallback to plain BM25 ordering is your backup generator. The lights may be dimmer, but the hospital keeps running.
Security Basics
Trained models and the data behind them deserve protection. Elasticsearch controls who may upload, view and delete trained models through roles and privileges tied to its machine-learning features. Give upload rights only to the small group or automated pipeline that manages models, and give the search service only the rights it needs to run searches. This follows the principle of least privilege, which means every person or program receives no more access than its job needs.
Logs deserve the same care. Search logs and click logs can contain personal information, such as what a named person searched for. Store them in a protected place, remove or mask personal identifiers before training, and keep them only as long as you need them. Follow the privacy laws that apply to your users.
Fairness and Accountability
A model that learns from human behavior can also learn human bias. If past users favored big brands, the model may push small sellers down until they never get seen. Review results for groups of items that should be treated fairly, and consider adding checks for things like seller diversity. Keep a record of which model version, features and data were used for each release, so you can explain a strange ranking later.
Learning to Rank in Elasticsearch is a feature of Elastic’s paid subscription levels and has evolved across versions. Before you plan a project, confirm the exact license level and the minimum Elasticsearch version in Elastic’s current documentation.
17Failure Scenarios and Recovery
Things will go wrong. Teams that plan for failures recover in minutes, and teams that do not may spend days in confusion.
| Scenario | Typical Symptom | Likely Cause | Recovery |
|---|---|---|---|
| Model missing or failing to load | Searches error out or come back with plain ordering | Wrong model name in config, model deleted, or a node without the model | Use the fallback query without rescoring, then fix the model name or re-upload the model |
| Latency spike | Slow searches at busy hours, high p99 | Window too big, too many features, or too little capacity | Lower the window with a config change, add replicas, or switch to a lighter model |
| Sudden quality drop | Click and purchase rates fall after a release | Bad model, feature mismatch or broken data pipeline | Roll back to the previous model, then compare logged feature values with training values |
| Slow quality decay | Metrics fall a little each month | Drift as catalog and behavior change | Retrain on fresh data and shorten the retraining schedule |
| Weird feature values | Top results look strange, such as every item having a rating of zero | An upstream job failed and wrote empty or wrong values into documents | Fix the data job, re-index the affected fields, and add data checks that block bad loads |
| Bias complaint | A group of sellers or items stops appearing | Behavior bias learned by the model | Review by group, adjust judgments or add fairness rules, and retrain |
Practice the Recovery
A rollback plan that has never been tried is only a hope. Schedule a rehearsal in a test environment, and include the fallback path where rescoring is switched off. Time how long it takes and write the steps down in a short runbook, which is a plain checklist that anyone on the team can follow at three in the morning.
Also decide in advance who can make the call. If latency has doubled and it is not obvious why, one named person should be allowed to lower the window or roll back without waiting for a long meeting. Quick, small actions reduce harm while the deeper cause is investigated.
Add a simple daily check that runs a few well-known test queries and compares the top results with expected ones. When a data job breaks, this tiny check often notices it hours before customers do.
18Monitoring, Logging and Metrics
A ranking model does not stay good on its own. The world changes, and monitoring tells you when the model has stopped keeping up.
What to Log for Every Search
To learn from searches later, you must record enough information today. For each search, log the query, the model version, the list of results shown with their positions, and a unique search identifier. For each click or purchase, log the same identifier and the position of the item. Without that link between what was shown and what was chosen, you cannot build judgments or correct for position bias.
Also log the feature values the model saw for the results that were shown. When a strange ranking appears, these numbers let you see exactly why the model preferred one document over another. It is like a flight recorder for search.
The Metrics That Matter
Latency Percentiles
Track p50, p95 and p99 response times, with and without rescoring, so any slowdown is caught early.
Click and Purchase Rates
Measure how often searches lead to a click or a purchase, and at which positions those actions happen.
Zero-Result and Abandon Rates
Count searches that return nothing, and searches where the person leaves without any action.
Errors and Fallbacks
Count how often rescoring fails and the fallback is used. A rising number is an early warning.
Feature Distributions
Watch the average and spread of key features. A sudden shift, such as ratings all becoming zero, usually means a broken data pipeline.
Offline Score Over Time
Re-score the current model on fresh judgments regularly to see whether its quality is slowly falling.
Drift: When the World Moves On
Drift happens when the world changes so that a model trained on old data no longer fits. New products appear, seasons change, a brand becomes trendy, or the site design changes where people click. A model trained in spring may rank winter coats badly in November. Regular retraining on fresh data, and alerts on falling quality metrics, keep the model up to date.
Set alerts on changes, not just absolute values. A click rate that drops by fifteen percent in one day is a story even if the number still looks normal. Elasticsearch also has a slow log that records searches exceeding a time limit, which is a handy way to spot the heavy queries behind latency spikes.
19Deployment and Cloud Considerations
A model that works on a laptop still has to reach production safely. This chapter covers the practical side of shipping and running LTR.
Where Elasticsearch Runs
You can run Elasticsearch in your own data center or on virtual machines in a public cloud, in containers managed by Kubernetes, or as a managed service such as Elastic Cloud, where Elastic operates the clusters for you. Managed services reduce day-to-day work like upgrades and node replacement, while self-managed clusters give more control over hardware and network. LTR works the same way in each, but the choice affects how you size nodes, handle upgrades and connect your training environment.
Separate Environments
Keep at least three environments: development, staging and production. In development, people experiment freely. In staging, a copy of production data and settings is used to rehearse releases and load tests. Production serves real users. A model should travel through these in order, and nobody should upload an untested model straight into production. This is like a theater rehearsing on a small stage before opening night.
Automating Model Releases
Manual uploads are error-prone. A better approach is a release pipeline, an automated series of steps that runs whenever a new model is approved. A typical pipeline looks like this:
- Pull the latest judgments and features, and train the candidate model.
- Run automated quality checks on held-out queries and compare against the current champion.
- Fail the build if quality falls below the agreed limit or if feature definitions do not match the shared contract.
- Upload the model to staging, run load tests and sample-query checks.
- After human approval, upload to production under a new versioned name.
- Start shadow scoring or a small experiment, and expand only when guardrails stay green.
Workflow schedulers such as Apache Airflow are often used to run the training steps on a schedule, and standard tools for building and testing software are used to run the checks. The particular tools matter less than the principle: every release should be repeatable, tested and reversible.
Sizing the Cluster
Capacity planning for LTR starts with your existing search load, then adds the extra rescoring work. Measure the extra time per search from a load test, multiply by your peak searches per second, and add a safety margin of at least twenty to thirty percent for unexpected spikes. Watch memory as well, because the model and the index data both live on the nodes that answer searches. If you use large models with many trees, confirm that heap and file-system cache are still comfortable.
Controlling Cost
Costs come from three places: the cluster that answers searches, the pipelines that collect and process data, and the people who label and review. Rescoring cost can be trimmed by shrinking windows, removing weak features and simplifying models. Data cost can be reduced by sampling logs sensibly and expiring old events. Labeling cost can be focused on the queries where the model is most uncertain, since grades there teach the most.
Example: A Safe Weekly Release
A retailer retrains its ranking model every Sunday night through an automated pipeline. On Monday morning, the staging checks have already passed, and the model receives ten percent of traffic. If click and purchase rates stay healthy through Tuesday and latency stays under budget, it expands to all traffic on Wednesday. The previous week’s model stays in the cluster until the following Sunday, so a rollback is a single setting change.
20Advantages, Disadvantages and Trade-offs
LTR is powerful, but it is not free. An honest look at both sides helps you decide when it is worth the effort.
Advantages
- Combines many signals at once with weights learned from data instead of guesses
- Improves the order of results without changing how documents are stored or found
- Adapts to changing behavior when retrained on fresh data
- Runs inside Elasticsearch, so no separate ranking service is needed
- Tree models are fast at query time and can be inspected for feature importance
- Replaces fragile tangles of hand-tuned boosts with one measurable model
Disadvantages
- Needs good judgments, which take time or traffic to gather
- Adds delay to each search, in proportion to window, features and model size
- Requires a training pipeline, monitoring and regular retraining
- Can learn bias from behavior data if not carefully checked
- Cannot rescue documents missed by the first stage
- Requires a paid subscription level of Elasticsearch
Key Trade-offs to Decide
Quality versus speed. A bigger window, more features and a larger model usually improve quality, but each one adds delay. The right point depends on how much delay your users tolerate and how much each improvement is worth to your business.
Freshness versus stability. Retraining often keeps the model current but adds operational work and raises the chance of a bad release. Retraining rarely is calm but allows drift. Many teams retrain on a fixed schedule, such as weekly or monthly, and retrain early when metrics fall.
Simplicity versus power. A model with a handful of features is easy to explain and cheap to run. A model with dozens of features can capture more patterns but is harder to debug. Start simple, and grow only where measurements show a clear gain.
Personalization versus privacy. Features about the individual person can improve relevance, but they require collecting and using personal data. Decide on this openly with your privacy and legal teams, and keep it to what users would reasonably expect.
21Design Patterns and Anti-Patterns
Teams that have shipped LTR many times keep repeating the same good habits and the same avoidable mistakes.
Patterns That Work
Retrieve Wide, Rerank Narrow
Keep the first stage generous so no good document is lost, then let the model order a modest window. This is the foundation of almost every LTR system.
Shadow Scoring
Run the new model on live searches in the background and log its scores without showing them to users. You can compare its choices with the current model’s before any risk is taken.
Champion and Challenger
Keep the proven model as the champion and test each new candidate against it on a small share of traffic. Only a clear winner takes over.
Feature Contract
Write down each feature’s exact meaning, unit, source and fallback value in one shared place, used by both training and serving. This prevents skew.
Anti-Patterns to Avoid
What people do
They train directly on raw clicks and treat every click as proof of relevance.
Why it hurts
Clicks are affected by position, by attractive images and by misleading titles. The model learns to repeat the old ordering and to favor clickbait, and quality slowly gets worse while click numbers look fine.
Better approach
Weight strong actions such as purchases more than clicks, correct for position bias, and keep a small human-graded set to catch drift away from real usefulness.
What people do
They set a huge window, such as several thousand documents, hoping the model will find hidden gems.
Why it hurts
Every extra document adds feature work on every shard. Latency climbs, the cluster needs more machines, and the extra documents rarely change the top ten anyway.
Better approach
Improve the first stage so good documents already rank in the top few hundred, and pick the smallest window that delivers the measured quality gain.
What people do
They use a feature during training that cannot be computed at search time, such as a value that is only known after the person clicks.
Why it hurts
This is called leakage. The model looks brilliant offline because it secretly saw the answer, and it collapses in production when that feature is missing.
Better approach
For every feature, ask one question: will this exact value be available at the moment the search runs? If not, remove it.
What people do
They ship a model once and never look at it again.
Why it hurts
Catalogs, seasons and user habits change. A model that was excellent last year slowly becomes average, and nobody notices because no one is watching the right numbers.
Better approach
Schedule retraining, monitor quality metrics, and treat the model as a living part of the product.
22Best Practices and Common Mistakes
A short checklist of habits that separate smooth LTR projects from painful ones.
Build a baseline first
Measure your current BM25 or hand-tuned ranking with a proper metric before doing anything else. Without a baseline you cannot prove the model is better.
Fix the first stage before the model
If good documents are not reaching the candidate list, no model can help. Check recall of the first stage using your judgments.
Start with few, meaningful features
Begin with a small set you can explain. Add features one group at a time and keep only those that improve held-out results.
Split data by query
Never let rows of the same query appear in both training and test sets.
Test online before full rollout
Use shadow scoring, then a small A/B test, then a gradual increase. Keep guardrail metrics visible throughout.
Keep rollback one step away
Retain the previous model in the cluster and control the active model name through configuration.
Common Mistakes
- Judging by a few favorite queries. Testing on the ten queries the team likes will hide problems on the thousand that nobody checks. Always use a broad sample.
- Ignoring rare queries. A model trained mostly on popular searches can be poor on the long tail, where many real people live.
- Changing many things at once. If you change the features, the window and the training data together, you cannot tell which change helped.
- Forgetting hard rules. Business or legal rules should be filters, not learned preferences.
- Treating offline gains as proof. A better offline score is a promising sign, and only a live experiment is proof.
23Learning to Rank Versus Other Ways to Improve Ranking
LTR is one tool among several. Choosing the right tool for the job saves effort.
| Approach | What It Does | Best For | Main Limit |
|---|---|---|---|
| Plain BM25 | Ranks by word matching alone | Small sites and exact-word search | Blind to popularity, freshness and meaning |
| Hand-tuned boosts | Adds fixed extra points for chosen conditions | A few clear business rules | Becomes fragile and hard to maintain as rules grow |
| Vector and hybrid search | Finds results by meaning as well as words | Natural-language queries and improving recall | Does not learn your business signals on its own |
| Semantic rerankers | A language model reads the query and each candidate text and judges how well they fit | Text-heavy content where reading comprehension matters | Focuses on text meaning and does not learn your popularity or business signals by itself |
| Learning to Rank | A model learned from judgments combines many signals into one score | Shops, marketplaces and any site with rich signals and behavior data | Needs judgments, a training pipeline and upkeep |
These approaches are not rivals. A common modern design uses hybrid search as the first stage to gather strong candidates, and then a learned ranker to order them using business and behavior signals. Text-based semantic rerankers can also be layered in where reading the content carefully matters. Elasticsearch offers several of these building blocks in recent versions, so check the current documentation for what your version supports.
If you have no behavior data and few signals, start with good BM25 and a few boosts. If your problem is missed matches because words differ, add vector or hybrid search. If you have plenty of signals and behavior data and your hand-tuned rules have become tangled, that is the moment Learning to Rank pays off.
The Older Plugin Route
Before Elasticsearch had a built-in feature, many teams used a community-built Learning to Rank plugin. It let you store feature definitions inside the cluster, log feature values for training, and upload models trained with popular tools. It is still discussed in older articles and used in some long-running systems. The plugin is separate software and must be matched to your Elasticsearch version, which can slow upgrades. When starting a new project, look first at the built-in feature and compare it to your needs.
24Real-World and Industry Examples
Learning to Rank is not a lab curiosity. Large organizations have publicly described using it to run search at scale.
Wikimedia: Ranking Articles for a Global Encyclopedia
The Wikimedia Foundation, which runs Wikipedia, has publicly described using Elasticsearch with a learning-to-rank approach to improve the order of search results across many languages. With so many topics and so many different reader needs, hand-tuned scoring rules were hard to maintain, and a learned model that combines text match with signals such as article popularity offered a more scalable path.
Etsy: Marketplace Search With Millions of Unique Items
Etsy has shared in its engineering writing how it applies learning to rank to marketplace search. A marketplace has a particular challenge: items are one-of-a-kind, listings change constantly, and buyers want quality, style and price that words alone cannot capture. Combining behavior signals such as clicks and purchases with listing quality signals is a natural fit for a learned ranker.
Airbnb: Ordering Stays for Each Traveler
Airbnb has published research on how it ranks listings, describing a journey that began with boosted tree models and grew to more advanced machine-learning methods. Its story shows a common path: start with tree-based ranking that is quick to build and easy to check, gather data, and move to heavier models only when the gains justify the cost.
Amazon: Product Search at Enormous Scale
Amazon’s search teams have published papers on ranking products with learned models. Product search blends text match, price, delivery promise, reviews and buying behavior, which is exactly the kind of many-signal situation where learned rankers beat hand-written formulas.
LinkedIn: Matching People and Jobs
LinkedIn has described using learning to rank in its talent and job search systems. When recruiters search for candidates or members search for jobs, the best result depends on skills, experience, location and past behavior all together, which a learned model can weigh far better than a fixed formula.
A Small Everyday Scenario
A mid-sized online bookstore stores two million books in Elasticsearch. Its first-stage search finds 300 candidates per shard for each query. A ranking model uses title match, author match, average rating, number of ratings, publication year and recent purchase rate. After three months of collecting behavior data, the team trains its first model, tests it on ten percent of visitors, and sees more people buying from the first page of results. The team keeps the old ordering as a fallback and retrains the model every month as new books arrive.
25Common Myths and Misunderstandings
Half-true ideas about Learning to Rank cause wasted effort. Here are the ones that appear most often, with the plain truth next to each.
“LTR replaces search.”
It does not. LTR sits on top of a normal search. Retrieval still finds the candidates, and the model only decides their order.
“More features always means better ranking.”
Extra features add cost and can add noise or leakage. Keep a feature only when held-out results improve and the added time is acceptable.
“The model will fix a weak first stage.”
A model cannot promote a document it never receives. Check the recall of the first stage before spending time on the ranker.
“Clicks are the truth.”
Clicks are useful but biased by position, presentation and curiosity. Combine them with stronger actions and human-graded samples.
“Once trained, the job is done.”
Catalogs, seasons and habits change. A model that is not monitored and retrained slowly loses quality without any visible error.
“A better offline score guarantees a better product.”
Offline scores depend on the judgment list, which may be flawed. Only a live experiment tells you what real people prefer.
“You need deep neural networks.”
Many excellent production systems run tree-based models. They are quick to train, quick to serve and easier to inspect, so they are a fine starting point.
“LTR is only for giant companies.”
Any site with enough behavior data and several useful signals can benefit. The real requirement is data and discipline, not a huge budget.
Treat every ranking idea as a hypothesis to be tested, including your own favorite ones. The teams that get the best results are not the ones with the cleverest models, but the ones with the most honest measurement.
26Glossary of Terms
A quick reference for every important word used in this guide, in plain language.
| Term | Plain Meaning |
|---|---|
| BM25 | The default text scoring formula in Elasticsearch, based on how often and how rarely query words appear in a document |
| Boost | A hand-chosen bonus added to a score when some condition is true |
| Candidate | A document found by the first stage that may be passed to the ranking model |
| Cascade | A chain of rankers where each later one is stronger and sees fewer documents |
| Click model | A statistical method that estimates true preference from clicks after correcting for position |
| Cold start | The situation where there is not yet enough behavior data for new sites or new items |
| Coordinating node | The node that receives a request, fans it out to shards and merges the answers |
| Drift | A gradual mismatch between a model and the world as data and behavior change |
| Eland | An open-source Python library from Elastic that helps upload trained models into Elasticsearch |
| Feature | One number describing a query and document pair, used as input to the model |
| First stage | The fast initial search that produces candidates before rescoring |
| Interleaving | An experiment method that mixes results from two rankers into one list and counts which one people prefer |
| Judgment | A grade that says how relevant a document is for a query |
| Leakage | Using information during training that would not be available at search time, which makes offline results falsely good |
| LambdaMART | A popular ranking method built from boosted decision trees that focuses on the top of the list |
| NDCG | A score between 0 and 1 that rewards putting highly graded documents near the top |
| Overfitting | Memorizing training examples so well that new examples are handled poorly |
| Position bias | The tendency of people to click higher results just because they are higher |
| Rescorer | The Elasticsearch component that reorders top candidates using a second scoring step |
| Shard | One piece of an index, stored on a data node and searched independently |
| Skew | A difference between how features are computed in training and in live serving |
| Window size | The number of top candidates per shard that the rescorer reorders |
27Frequently Asked Questions
Quick answers to the questions people ask most often when they first meet Learning to Rank.
28Summary and Key Takeaways
Key Takeaways
- LTR teaches a model how to order results. It learns from graded examples of good and bad answers instead of relying on hand-written scoring rules.
- Search works in two stages. A fast first stage finds candidates, and the LTR model reorders only the top window of them, so quality of the first stage sets the ceiling.
- Three ingredients drive everything. Well-chosen features, trustworthy judgments and a tree-based model, with features measured identically at training and serving time.
- Training happens outside Elasticsearch. You train offline, upload the model with Eland, and let the rescorer run it on each shard at query time.
- Measure before and after. Use offline metrics such as NDCG, then live A/B tests with guardrails, and keep rollback one configuration change away.
- Balance quality against speed. Window size, number of features and model size are the three dials that control latency and cost.
- Treat the model as a living system. Monitor drift, retrain on fresh data, protect the logs and review results for fairness.