While studying Retrieval-Augmented Generation (RAG) and Amazon Bedrock Knowledge Bases, I came across the familiar architecture: documents are ingested, split into chunks, converted into embeddings and stored in a vector store. When someone asks a question, the system retrieves relevant content and gives it to a large language model (LLM).

I understood the flow at a high level, but I wanted to understand what the retrieval component actually does. Instead of starting with an embedding model or a vector database, I decided to go back to something familiar: Python data structures.

On 27 November 2026, I worked through a small document retrieval exercise using a list of dictionaries, document IDs, sequential search and metadata filtering. This post explains that learning step, not a deployed RAG system. The code below is a simplified, fictional example of the approach rather than a copy of my original exercise.

1. Where does retrieval fit in RAG?

Two paths: source documents become chunks, embeddings and indexed records; a question retrieves relevant chunks and supplies them to an LLM. A local Python exercise covers only document lookup and filtering.

Figure 1 — A simplified vector-based RAG architecture, with the retrieval component isolated as a learning exercise. The diagram is conceptual, not a record of an AWS deployment.

There are two distinct activities in this type of architecture:

Activity What happens
Ingestion Prepare documents, split them into chunks, generate vector embeddings and index the content with associated metadata.
Query Represent the question for search, retrieve relevant chunks, and provide their text as context to the LLM.

An embedding is a numerical representation of content. An embedding model makes it possible to compare a question and document chunks by their representations, rather than requiring an exact document ID or an exact word match. A vector store can index those representations and help find similar ones. The original content or a reference to it still matters: the retrieved text is what can help ground an answer.

My first exercise did not do semantic search. I wanted to understand a simpler question first: given a collection of documents, how do I find the right record and select the documents that satisfy a metadata condition?

2. Model a document before searching it

I used a Python list to represent a collection, with each document represented by a dictionary. For this public example, the records describe imaginary service documents; there is no employer or customer data.

documents = [
    {"id": "D001", "visibility": "public",
     "text": "Service status is available on the status page."},
    {"id": "D002", "visibility": "internal",
     "text": "Example internal maintenance checklist."},
    {"id": "D003", "visibility": "public",
     "text": "Users can subscribe to service notifications."},
]

The document ID identifies a record. The text is its content. The visibility field is metadata that describes the record. A real knowledge base could also store a source reference, document version or effective date.

3. Retrieve a document by its ID

The simplest implementation is to iterate through the list and return the first matching record.

def get_document_by_id(document_id):
    for document in documents:
        if document["id"] == document_id:
            return document
    return None

The return inside the loop matters. Once I find the matching ID, there is no need to continue searching. Returning None after the loop provides an explicit result when the ID is not present.

Assuming fixed-size IDs and ordinary dictionary field access, the best case is O(1) when the first document matches. The worst case is O(n) when the match is last or absent. Here, n is the number of documents. This is a complexity analysis, not a measured performance benchmark.

4. Filter using metadata

Searching by ID and filtering a collection are different operations. For the second operation, I want all public records, not just the first one.

def get_public_documents():
    result = []
    for document in documents:
        if document.get("visibility") == "public":
            result.append(document)
    return result

In this example, D001 and D003 should be returned. D002 should be excluded. A missing visibility field also fails the condition. The function visits every document, so its time complexity is O(n). If k documents match, the result list holds O(k) references.

Metadata filtering is not the same as authorisation. This function demonstrates a selection rule, not a security boundary. For example, the ID lookup above can still return D002. In a real application, the caller’s verified identity and permissions must govern every retrieval path before content is exposed or supplied to the model. A client-provided visibility flag alone is not sufficient. The toy example contains only fictional data.

5. A small reproducible exercise

The following is a suggested local test for readers. Save the three Python blocks above, in order, into mini_retrieval.py, then append these checks:

assert get_document_by_id("D001")["id"] == "D001"
assert get_document_by_id("D003")["id"] == "D003"
assert get_document_by_id("D999") is None
assert [doc["id"] for doc in get_public_documents()] == [
    "D001", "D003"
]
assert all(doc["visibility"] == "public"
           for doc in get_public_documents())
print("Retrieval and filtering checks passed")

Run it using a local Python 3 interpreter:

python3 mini_retrieval.py

Expected output, if all assertions pass and Python assertions are enabled:

Retrieval and filtering checks passed

This does not require an AWS account, API keys, paid services or external Python packages. The assertions cover a first match, last match, missing ID and the public-only filter. They do not establish production security or semantic relevance.

6. Why not use a dictionary indexed by document ID?

This was the natural next question in my learning session. If the system frequently retrieves documents by unique ID, I could maintain a dictionary with the ID as its key rather than scan the list every time.

Approach Lookup Trade-off
Sequential list search O(n) worst case Simple, no separate index
Dictionary keyed by ID O(1) average case O(n) initial construction and additional memory; unique IDs and updates need handling
Vector similarity search Depends on index and query configuration Finds related content without knowing its ID; introduces embedding and indexing considerations

Dictionary lookup is a useful next exercise, not an implementation I completed in this learning step. It also solves a different problem from vector search. If I have an exact ID, hashing can help me find it quickly; if I have a natural-language question and do not know which document answers it, exact ID lookup alone cannot help.

7. Relating this to Amazon Bedrock Knowledge Bases

AWS provides a useful separation in its Knowledge Bases APIs. Retrieve can return relevant source chunks without generating an answer. RetrieveAndGenerate combines retrieval with generation, supplying retrieved context to a model. Metadata filters can constrain retrieval on supported knowledge base configurations. Supported operators and search behaviour depend on the knowledge base and store configuration.

From a system design perspective, I would like to be able to inspect retrieval results before judging the generated answer. If the retrieved context is missing, outdated or irrelevant, changing the prompt may not address the underlying problem.

For a future AWS exercise, I would start with a small fictional dataset and test the Retrieve operation separately. I would check which source chunks were returned, whether the metadata condition behaved as intended, and what happened with a question for which the dataset had no relevant content. Only then would I add generation. I have not performed this AWS knowledge base lab yet.

8. What I verified and what remains

Item Status at the end of my 27 November learning exercise
List of dictionaries and document IDs Implemented in the learning exercise and reviewed by inspection
Sequential lookup and early termination Reviewed by inspection
Public-document metadata filtering Reviewed by inspection
Execution of the original exercise / automated tests Not independently confirmed in my learning record
Dictionary index Next suggested exercise
Chunking, embeddings, vector database, LLM and AWS deployment Not implemented in this stage

The code in this article is a standalone teaching example, not evidence of a production system or of a completed Bedrock deployment.

Takeaway

I do not need to introduce every component of an architecture at once. By isolating retrieval, I can practise Python, reason about algorithmic complexity and understand one part of RAG before adding a vector index or an LLM.

For me, the engineering lesson is to make a component understandable and testable before adding complexity around it. In a design review, I would ask what the retrieval component is expected to return, how we would test it, and how we would prevent unauthorised content from reaching the model.

References

This is a personal learning record. The documents, example code and diagram are illustrative; no production system or customer data is represented.