LlamaIndexllama-index-core 0.14 · Python 3.10+
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
30 small wins to finish your pathNext lesson →

Retrievers: the search step on its own

A retriever is the search step of a RAG system: given a question it returns the chunks whose meaning is closest, each with a similarity score.

Last updated: 28 Sep, 2026 · LlamaIndex 0.14

Parts 1 and 2 built and chunked an index. From here the course queries it. Before a model ever sees the documents, the retriever decides which chunks it gets, so most wrong answers start here.

Building a retriever from the index

as_retriever turns the index into a retriever. similarity_top_k is how many chunks come back. retrieve returns a list of NodeWithScore: the chunk, and its cosine similarity to the question.

python
retriever = index.as_retriever(similarity_top_k=3)
results = retriever.retrieve("Do used bulbs get refunded?")
for result in results:
    print(result.score, result.metadata["file_name"])
Project files used on this pageThis lesson builds on a project from earlier lessons. The code below imports these files. Click a file to see its code, or follow the link to the lesson that wrote it. To run the code yourself, keep them in the same folder.
View the code here
help/lamps.md
# Lamps

The LMP-204 desk lamp has a known cable fault. Stop using a lamp with a damaged cable and we will replace it free of charge.

All lamps come with a two year guarantee against electrical faults.

Bulbs are not covered by the refund policy once they have been used.

The LMP-310 floor lamp needs a bulb with an E27 fitting, which is sold separately.
help/refunds.md
# Refunds

You can get a full refund within 30 days of delivery. The money goes back to the card you paid with within 5 working days of us receiving the item.

Items bought in a sale can be refunded too, but the delivery charge is not returned.

To start a refund, open the order in your account and choose Return an item. Print the label and drop the parcel at any post office.

Personalised items cannot be refunded unless they arrive damaged.
help/delivery.md
# Delivery

Standard delivery takes 3 to 5 working days and is free on orders over 40.

Express delivery arrives the next working day if you order before 2pm. It costs 6.

We deliver to the mainland only. Parcels to islands take 2 extra working days.

If a parcel has not arrived after 10 working days, contact us and we will send a replacement.

Retrieving for a bulb refund question

Example
retriever = index.as_retriever(similarity_top_k=3)
results = retriever.retrieve("Do used bulbs get refunded?")
for result in results:
    print(f"{result.score:.3f}", result.metadata["file_name"], "|", result.get_content().replace("\n", " ")[:60])

Reading the scores

  • The top chunk is the answer. The used-bulbs sentence in lamps.md scored highest and came first.
  • The refund document is close behind. The question says refunded, and a chunk about refunds in general is similar in meaning, so it ranks second.
  • Scores are cosine similarities. Higher means closer meaning. The gap between first and second is what a cutoff will later act on.

Retriever vs query engine

PieceWhat it returns
retriever.retrieve(q)The chunks and their scores. No model is called.
query_engine.query(q)An answer built from those chunks by a model, covered next.

When to look at the retriever

  • An assistant answered wrongly and you need to know whether the right chunk was even retrieved.
  • You are choosing a chunk size or top-k and want to see what each returns.
  • A question uses different words from the documents and you want to confirm meaning-based search still finds it.
Watch out
Top-k always returns k chunks, whether or not any of them answers the question. A result coming back is not proof the answer is in it. Read the score, and the cutoff in Refusing when nothing fits: similarity cutoffs turns a weak score into a refusal.
Try it yourself
  • Ask "When will my express order arrive?" and read the top chunk.
  • Set similarity_top_k=1 and ask about used bulbs again.
  • Rebuild the nodes with chunk_size=400 and compare the scores.

This is what real progress feels like.