Evaluating search and RAG: precision, recall, MRR and nDCG

You changed the chunk size in your RAG system, or turned on hybrid search. Did it really get better? If you try a few questions and decide it "feels better," you miss the questions that got worse. Press 10 search results to flip them between right and wrong, and work out the numbers that measure search.

Short answer. Whether search worked is a number, not a feeling. Precision is the share of what you fetched that is right. Recall is the share of the right ones that you fetched. MRR and nDCG also look at how high up the right ones are. For RAG, you measure the search and the quality of the answer (does it stick to the sources, does it answer the question) separately.

    Each box is one search result, and #1 is at the top. Press a box (or Tab to it and press Enter) to flip it between right and wrong. Boxes below k are faded and do not count. Next to a right one, P@3 = 2/3 means "if you read down to #3, 2 of the 3 are right." AP uses these values. "All right answers" also counts right answers that the search did not return. If you mark more results right, that number goes up with them.

    Try this. ① The starting layout (k = 5) gives precision 0.60, recall 0.75, F1 0.67, RR 0.50, AP 0.44 and nDCG 0.59. These are the same numbers as the worked example in section 7. ② Raise k to 10. The right answer at #8 comes in, so recall becomes 1.00, and precision drops to 0.40. ③ Switch between "Right ones on top" and "Right ones at the bottom" (both at k = 10). Precision 0.30 and recall 1.00 stay the same. But RR is 1.00 against 0.13 (1 ÷ 8), AP is 1.00 against 0.22, and nDCG is 1.00 against 0.42. ④ "None right" gives 0 for every measure, and "All right" gives 1 for every measure. ⑤ In the starting layout, type 1 into "All right answers." The list already holds 4 right answers, so a note says it counted 4.

    Definitions

    The text below explains these with 10 search results. These are the textbook definitions. Think of the results a search returns from the top for one question. reli is the relevance of the result at rank i. For precision, recall, MRR and AP it is 1 if right and 0 if wrong. For DCG and nDCG it is a grade (0 to 3 on this page). G is the set of all answer chunks for that question, and Q is the set of questions in the evaluation set.

    precision at k
    The share of the top k results that are right.

    P@k=1k∑i=1kreli

    recall at k
    The share of all answers that appear in the top k. The denominator includes answers that did not show up in the results.

    R@k=1|G|∑i=1kreli

    reciprocal rank, mean reciprocal rank
    The reciprocal rank RR is one over the rank of the first right answer, and 0 if there is no right answer in the top k. The mean reciprocal rank MRR is the mean of RR over the questions. rankq is the rank of the first right answer for question q.

    MRR=1|Q|∑q∈Q1rankq

    average precision (AP)
    At each rank i where a right answer appears, take the precision up to that rank, P@i. Add these and divide by the number of all answers. An answer that never appears counts as adding 0. The mean of AP over the questions is MAP.

    AP=1|G|∑i=1kreli·P@i

    discounted cumulative gain (DCG)
    The sum of the relevance of the top k results, each cut down by dividing by log2(i+1) so that lower ranks count less. Some forms use 2reli−1 in the numerator to count high grades more.

    DCG@k=∑i=1krelilog2(i+1)

    normalized DCG (nDCG)
    DCG@k divided by the ideal DCG@k (IDCG@k). IDCG@k is the DCG@k when the answer chunks are placed from the highest grade down. It is between 0 and 1, and this page sets it to 0 when IDCG@k is 0.

    nDCG@k=DCG@kIDCG@k

    1. Why measure with numbers

    Building RAG means making a lot of choices. How big to cut the chunks (Chunking). Whether to use only vector search or mix in keyword search (Vector search, Hybrid search). How many chunks (k) to add. Whether to rerank. If you try a few questions after each change and decide it "feels better," you miss two things.

    For RAG it matters even more to measure search on its own. The LLM cannot see a chunk the search missed (RAG). When an answer is wrong, you need to know whether search or the LLM caused it, so you know what to fix. This page covers measuring search first, and measuring answers in section 10.

    2. Building a test set

    To compute any of these numbers, you first need a table that says "the right chunks for this question are these." That is the test set. Each line holds one question and the IDs of its right chunks.

    { "question": "How many days do I have to ask for a refund?", "relevant": ["refund-01", "refund-03"] }
    { "question": "Is the store open on Mondays?", "relevant": ["hours-02"] }
    1. Collect questions. Questions real users asked are best. If you have none yet, a person reads the documents and writes them. You can also give an LLM a chunk and have it write questions. But questions written while looking at a chunk tend to reuse its exact words, so they can be easier than real ones. A person reads them and picks.
    2. A person decides the right chunks. For each question, write down every chunk that holds the answer. If the answer is split across two chunks, write both. If you also grade how related each one is (0 to 3), you can use nDCG from section 5.
    3. Start with a few dozen. You do not need hundreds at first. Mix common questions, ones that need an exact match like a product code, ones phrased in their own words, and ones whose answer is not in the documents.
    4. Keep adding failures. When a real question goes wrong, add it to the test set. Then you can see if the same mistake comes back.

    One thing to watch. When you change the chunk size, the chunks get cut again and their IDs change. If you saved the right answers only as chunk IDs, you have to label everything again. If you save them as "this document, this sentence," you can find which new chunk holds that sentence with code.

    3. Precision, recall and F1

    Say the search returns k results from the top. Each result is either right (a right chunk) or wrong.

    precision@k = right ones in the top k ÷ k
    recall@k    = right ones in the top k ÷ all right answers
    F1          = 2 × precision × recall ÷ (precision + recall)

    Hit rate@k is even simpler. It is 1 if at least one right answer is in the top k, and 0 if not. You average it over many questions. For a question with only one right answer, hit rate@k and recall@k are the same.

    This page makes two choices. If there are fewer than k results, the missing places count as wrong (it still divides by k). If a question has no right answers at all, recall would divide by 0, so it cannot be defined, and this page shows 0. Tools can make different choices. Check before you compare numbers from somewhere else.

    4. Measures that look at order: RR, MRR and AP

    Precision@k and recall@k do not look at the order inside the top k. Whether the 3 right ones are at #1 to #3 or at #8 to #10, the numbers are the same at k = 10. But people read from the top and stop after a few. You need a measure that also looks at order.

    RR  = 1 ÷ place of the first right answer   (0 if none in the top k)
    MRR = the average of RR over the questions
    AP  = (sum of "precision so far" at each place with a right answer) ÷ all right answers
    MAP = the average of AP over the questions

    RR and AP are numbers for one question. Average them over many questions and you get MRR and MAP. The M (mean) in front means "the average over the questions."

    5. When relevance has grades: DCG and nDCG

    So far there were only two kinds: right and wrong. In practice you get "a perfect chunk" mixed with "a somewhat related chunk." If you give relevance a grade, you can count that difference. This page uses four levels: 0 (not related), 1 (a little), 2 (fairly), 3 (exactly right). The original paper behind this measure also used four levels of grades.

    The idea has three steps.

    1. Cumulative gain (CG). Add up the grades in the top k. The more good ones you fetch, the bigger it gets. But it ignores order.
    2. Discounted cumulative gain (DCG). The lower the place, the more you shrink its share before adding. People read less the further down they go.
    3. Compare with the best (nDCG). Divide by the DCG you would get with all the relevant chunks in the best order, highest grade first (IDCG). Relevant chunks missing from the results count too. The result is between 0 and 1, and 1 means no better order exists.
    share of a place = grade ÷ log₂(place + 1)
    DCG@k  = sum of the shares in the top k
    IDCG@k = DCG@k with the grades of all relevant chunks sorted from highest to lowest
    nDCG@k = DCG@k ÷ IDCG@k        (0 if IDCG is 0)

    To see this formula built one piece at a time, with pictures, go to section 6.

    The divisor log₂(place + 1) is 1 at #1, 1.58 at #2, 2 at #3, 3 at #7 and 3.46 at #10. It grows slowly. So the gap between #1 and #2 is big, and the gap between #9 and #10 is small.

    Why divide by IDCG? DCG comes out at different sizes for different questions. A question with many right answers gets a big DCG. A question with one right answer can never go above that one grade, however well you rank it. Average them as they are, and the questions with many answers decide the average. Divide each by the biggest value that question could reach, and every question lands between 0 and 1, so you can average them.

    You can use nDCG with only right and wrong too (grades 0 and 1). The nDCG in the top lab is that case. There, IDCG puts as many 1s as there are right answers at the very top. Right answers missing from the results count in IDCG too.

    The exact formula differs from place to place. A common version uses 2^grade − 1 instead of the grade, to reward high grades more (a grade 3 is worth 7, not 3). When the grades are only 0 and 1, both versions give the same value. Before you compare nDCG numbers from somewhere else, check which version they use.

    Each row is one result. Each press on a grade button changes it 0 → 1 → 2 → 3 → 0. The green bar (now) is that place's share in the current order (grade ÷ divisor). The blue bar (best) is the share at that place if the same grades were in the best order. This lab assumes every right answer is among these 10 and uses k = 10.

    Try this. ① The starting layout gives DCG 6.44, IDCG 7.50 and nDCG 0.86. ② The grade 3 at #5 only gets 3 ÷ 2.58 = 1.16. In the best order, the two grade 3s go to #1 and #2 and get 3.00 and 1.89. ③ "Sort into the best order" gives nDCG 1.00, and "Worst order" gives 0.51. Even the worst order is not 0, because the right chunks were still fetched. ④ In the starting layout, press #3 (grade 0) once to make it 1. DCG goes up to 6.94 and IDCG to 7.83, and nDCG becomes 0.89. One more right answer raises the best possible value too. ⑤ Press "All 0." IDCG is 0, so a note says nDCG is shown as 0.

    6. How the formula is built: nDCG

    Here the formula from section 5 is built one piece at a time. It uses the starting layout of the lab above (grades 2, 3, 0, 1, 3, 0, 2, 0, 0, 1, k = 10). In the formula, relᵢ is the grade (relevance) of the result at place i, and Σᵢ means "add up from place 1 to place k."

    Press "Next" to go one step at a time. A piece is added to the formula on top (the orange background marks the new piece), the bars change by that much, and the reason appears below. The dashed outline is the length before shrinking. The bottom bar is the sum (Σ): the values of the places laid end to end from the left.

    1. Add up the grades: CG@k = Σᵢ relᵢ. Counting right ones like precision does gives 6 (precision@10 = 0.60), and a perfect result (grade 3) counts the same as a slightly related one (grade 1). So add up the grades instead: 2 + 3 + 0 + 1 + 3 + 0 + 2 + 0 + 0 + 1 = 12. Why: better results weigh more. But adding ignores order. Put the same grades in the worst order and precision is still 0.60 and the sum is still 12.
    2. Shrink the lower places: DCG@k = Σᵢ relᵢ ÷ log₂(i + 1). The divisor is 1 at #1, 1.58 at #2, 2.58 at #5 and 3.46 at #10. The same grade 3 gives 1.89 at #2 but 1.16 at #5. The sum drops from 12 to 6.44. Why: people read from the top and stop, so lower places get read less. Now the order shows in the number. The same grades in the worst order (0, 0, 0, 0, 1, 1, 2, 2, 3, 3) give a DCG of 3.81. Using i + 1 instead of i keeps #1 from shrinking (log₂ 2 = 1. With i itself, log₂ 1 = 0 and you would divide by 0).
    3. The value of the best order: IDCG@k. It is the DCG of the same grades sorted from highest to lowest (3, 3, 2, 2, 1, 1, 0, 0, 0, 0): 3.00 + 1.89 + 1.00 + 0.86 + 0.39 + 0.36 = 7.50 (blue bars). Why: DCG 6.44 alone does not tell you if that is good. A question with a single grade 3 result gets DCG 3.00 with that result at #1, and no order could be better. Compare the raw numbers and the perfect order comes out lower.
    4. Divide, and it is done: nDCG@k = DCG@k ÷ IDCG@k. 6.44 ÷ 7.50 = 0.86. The scale of the sum bar changes from 0 to 12 into 0 to 1, so the blue bar reaches the end. Why: the sum is biggest when the high grades sit in the top places, which shrink least, so no order has a DCG above IDCG (the rearrangement inequality, not proved here). That keeps nDCG between 0 and 1, and 1 means no better order exists. The single grade 3 question gets 3.00 ÷ 3.00 = 1.00, so the two questions can be compared and averaged. The worst order gives 3.81 ÷ 7.50 = 0.51, the same as "Worst order" in the lab above.

    7. Work it out by hand: five results

    Here is one small example with 5 results, and every measure worked out. This question has 4 right answers, but only 3 of them are in the top 5. (The fourth is at #8 in the top lab.)

    Place12345
    Result✗ wrong✓ right✓ right✗ wrong✓ right
    Divisor log₂(place + 1)11.5822.322.58
    1. Precision@5. 3 are right. 3 ÷ 5 = 0.60.
    2. Recall@5. 3 of the 4 right answers. 3 ÷ 4 = 0.75.
    3. F1. 2 × 0.60 × 0.75 ÷ (0.60 + 0.75) = 0.90 ÷ 1.35 = 0.67.
    4. RR. The first right answer is at #2. 1 ÷ 2 = 0.50.
    5. AP@5. Right answers are at #2, #3 and #5. Precision so far at those places is 1/2 = 0.50, 2/3 = 0.67 and 3/5 = 0.60. They add up to 1.77, and you divide by all right answers, 4. 1.77 ÷ 4 = 0.44. You divide by 4, not 3, because the fourth right answer was never found and counts as 0.
    6. nDCG@5. Treat right as 1 and wrong as 0. DCG = 1 ÷ 1.58 + 1 ÷ 2 + 1 ÷ 2.58 = 0.63 + 0.50 + 0.39 = 1.52. The best order puts the 4 right answers at #1 to #4. IDCG = 1 + 0.63 + 0.50 + 0.43 = 2.56. nDCG = 1.52 ÷ 2.56 = 0.59.
    Precision@5Recall@5F1RRAP@5nDCG@5
    0.600.750.670.500.440.59

    The "Starting layout" in the top lab is this example. Try making #1 right. Precision and recall go up, but so does everything else: RR becomes 1, and AP and nDCG rise a lot.

    8. The precision-recall trade-off, and measuring many questions

    What happens as k grows?

    So the two trade off. If you only watch recall, just keep growing k. If you only watch precision, return only the single surest result. Both are easy tricks. Search really got better when both go up together at the same k. In the right-hand chart below, that means the precision-recall curve moves toward the top right.

    In RAG this trade-off shows up as money and quality. A big k makes it more likely the right chunk gets in (recall), but the prompt gets longer, which costs money and time, and wrong chunks get mixed in (RAG). So measure recall@k for each k, and pick the smallest k where recall is high enough.

    Precision and recall as k changes
    Precision-recall curve

    The left chart shows the chosen question's precision (green) and recall (blue) for k = 1 to 10. The orange dashed line and the orange dots mark the current k. The right chart puts the same values on a precision-recall curve, with recall across and precision up. Press a question's name in the table to show it in the charts. The ✓ and ✗ marks in the table are the 10 results, and the ones below k are faded. The 5 questions and their results are made up.

    Try this. ① Pick "Refund deadline" and raise k from 1 to 10. Recall goes 0, 0.25, 0.50, 0.50, 0.75, 0.75, 0.75, 1.00, 1.00, 1.00 and never goes down. Precision goes up and down, 0, 0.50, 0.67, 0.50, 0.60, and ends at 0.40 at k = 10. ② "Open on Mondays" has one right answer, and it is at #1. Recall is 1 from k = 1, and raising k only drops precision from 1.00 to 0.10. For a question with one right answer, precision@k can be at most 1 ÷ k, so it tells you little. Use RR and hit rate for those. ③ At k = 5, the 5 questions give MRR@5 (0.50 + 1 + 0.25 + 0 + 1) ÷ 5 = 0.55, hit rate@5 0.80, mean recall@5 0.67 and mean nDCG@5 0.54. "Shipping fee" alone scores 0 on everything and pulls the averages down. ④ Raise k to 10. The right answer for "Shipping fee" gets caught at #7, so hit rate@10 becomes 1.00 and MRR@10 becomes 0.58.

    The precision-recall curve zigzags because a wrong result keeps recall the same and only lowers precision, while a right result raises both. Textbooks sometimes smooth the curve by using "the highest precision at this recall or any higher recall" (interpolated precision).

    Measure a set of questions, not one

    The number for one question jumps around. Look only at "Points expiry" and RR@5 is 0.25, which looks bad. Look only at "Open on Mondays" and it is 1, which looks perfect. When you change a setting, what you look at is the average over the whole test set. MRR is RR worked out for each question and then averaged, and mean nDCG is made the same way. Every question gets the same weight.

    An average can hide things too. Even if the average stays the same, some questions may have gotten better and others worse. After a change, look at the list of questions that got worse along with the average.

    9. Which measure to pick

    SituationLook at firstWhy
    One right answer (opening hours, finding one document)MRR, hit rate@kWhere the first right answer lands is all that matters. Precision@k can be at most 1 ÷ k
    Many right answers and you must not miss any (legal clauses, research)recall@k, MAPHow many you missed matters most
    Relevance varies and the top order matters (search pages, recommendation lists)nDCG@kIt looks at grades and order together
    RAG searchrecall@k (k = chunks you put in the prompt)The LLM can only read the right chunk if it is among those k. "Did it get in" comes before order
    RAG where most questions have one right chunkhit rate@k, MRROne chunk getting in is enough, and the higher it is, the smaller k can be
    A page where people only look at the top fewprecision@k (k = results on screen)What people see needs to be clean

    10. Evaluating RAG answers

    Good search scores do not guarantee a good answer. You can hand the LLM the right chunk and it can still answer from what it already knows, make up things that are not in the chunk, or answer only part of the question. That is why RAG measures search and answers separately.

    ItemWhat it asksWhat it looks at
    FaithfulnessIs every sentence of the answer backed by the chunks you gave? Did it make up anything that is not in them?answer + chunks given
    Answer relevanceDoes the answer answer the question? Is anything off-topic or missing?question + answer
    Context relevanceAre the chunks you gave useful for the question? Are useless chunks filling the prompt?question + chunks given

    The three point to different problems. Low context relevance means fix the search (chunking, hybrid search, k). Good context but low faithfulness means fix the LLM side (instructions, model). High faithfulness but low answer relevance means it read the right chunks and still answered something else. If the test set has model answers, also check whether the answer matches them. For questions whose answer is not in the documents, check separately that it says the documents do not cover it.

    Grading by people

    This is the method you can trust most. First write a scoring guide (for example, faithfulness 2 = every sentence is backed, 1 = some sentences are not, 0 = it made up something important). Have two people grade the same few dozen answers on their own, talk through the ones they graded differently, and fix the guide. If people cannot agree on a rule, a machine will not match it either. It is slow and expensive, so you cannot do a lot of it.

    Using an LLM as the grader

    Give an LLM the question, the chunks you gave, the answer and the scoring guide, and get back a score and a reason. It is fast and cheap, so you can run it every time you change a setting. But the grader makes mistakes too.

    11. Offline evaluation and real usage metrics

    Everything so far is offline evaluation. You use a test set made ahead of time, before release, and compare under the same conditions. Real users leave different numbers behind.

    Offline evaluationReal usage metrics
    Measured withA test set (questions + right answers)What real users do
    Example numbersrecall@k, MRR, nDCG, faithfulnessShare of results clicked, share of people asking the same thing again, thumbs up or down, asking for a human agent
    Good atFast. Works before release. Compares two settings under the same conditionsReal questions from real users
    Weak atThe test set may not look like real questionsSlow to build up and noisy. It does not tell you why

    Read real usage metrics with care. People click the top result a lot, so many clicks do not mean it was the right answer. Asking again is often a sign the first answer fell short, but it can also be a new follow-up question.

    Use the two together. Offline evaluation filters out settings that make things worse. Release the ones that pass to some users first and compare real usage metrics (an A/B test). Then collect the questions that failed in real logs and add them to the test set.

    12. Do not let the test set leak

    Picking settings with a test set has a trap. Look at the same 50 questions while changing the chunk size, k and weights dozens of times, and you end up picking the setting that fits those 50 questions. It is like studying with the exam questions in hand. The numbers go up, but new questions may not get any better. It is the same thing as overfitting.

    13. Key properties

    PropertyWhat it means
    Every measure is between 0 and 11 is best. If no right answer is in the top k, all of them are 0
    Recall@k never goes down as k growsFetching more only reduces what you missed. Fetch everything and it is 1
    Precision@k and recall@k ignore the order inside the top kRR, AP and nDCG look at order
    Recall, AP and nDCG need the number of all right answersThe test set must list right answers even if the search never returns them
    The number for one question jumps aroundCompare averages over the whole test set (MRR, MAP, mean nDCG)

    Where is it used?

    Common questions

    What is the difference between precision and recall?

    Precision is the share of what you fetched that is right. Recall is the share of the right ones that you fetched. Fetch more, and recall goes up while precision usually goes down.

    What is the difference between MRR and nDCG?

    MRR only looks at where the first right answer is for each question. nDCG looks at the place of every right answer in the top k, and uses relevance grades if you have them. MRR fits questions with one right answer, nDCG fits questions with many.

    Which measure should I use for RAG search?

    Look at recall@k first, where k is the number of chunks you put in the prompt. The LLM can only read the chunk with the answer if it is among those k. If most questions have only one right chunk, also look at hit rate@k and MRR.

    How many test questions do I need?

    Start with a few dozen and add more over time. With few questions, one or two of them can swing the numbers a lot. When two settings come out close, add questions and measure again.

    Can I let an LLM do the grading?

    Yes, after you check it against grades from people. LLM graders make mistakes too, and studies report biases such as favoring the answer shown first or the longer answer. Changing the grading model or its instructions changes the scores, so keep them fixed.

    Words

    Word used hereWord used in textbooks
    share of what you fetched that is rightprecision. Cut at the top k: precision@k (P@k)
    share of the right ones that you fetchedrecall, recall@k
    harmonic mean of precision and recallF1 score, F-measure
    is at least one right answer in the top khit rate, success@k
    1 ÷ place of the first right answerreciprocal rank (RR)
    average of RRmean reciprocal rank (MRR)
    precision at each right answer, gathered upaverage precision (AP). The average over questions is MAP (mean average precision)
    relevance grades / only right or wronggraded relevance / binary relevance
    grades added up / added with lower places shrunkcumulative gain (CG) / discounted cumulative gain (DCG)
    DCG of the best orderideal DCG (IDCG)
    DCG ÷ IDCGnormalized DCG (nDCG)
    test set / the right answers for each questiontest collection, evaluation set / relevance judgments (qrels)
    precision-recall curve / the smoothed oneprecision-recall curve / interpolated precision
    averaging with every question weighted the samemacro-average
    sticking to the sourcesfaithfulness, groundedness
    does the answer fit / do the chunks fitanswer relevance / context relevance
    using an LLM as the graderLLM-as-a-judge
    measuring with a ready-made set / measuring with real useoffline evaluation / online metrics, A/B test
    development questions / test questionsdev set / test set

    Sources

    ← Keyword and hybrid searchAI agents →