Updating and Maintaining a Knowledge Base

Follow a complete refund policy update to replace a document’s old chunks in Qdrant and query the results to verify the change.

🎯 Introduction

Suppose we have stored a Membership Refund Policy in Qdrant that says:

Members can request a refund within 7 days of purchase.

The company later extends the refund window from 7 days to 14 days.

The knowledge base now needs updating. Simply adding the new 14-day rule leaves the old 7-day rule in place, so the next user question could retrieve two conflicting policies.

We therefore need to replace the old refund policy with the new version, rather than just add another passage.

We will first store an old policy, update it to the new version, and query Qdrant to confirm that the old rule is gone.


🧱 Find All Chunks Belonging to a Document

As we learned earlier, a document can be split into multiple chunks, each stored as a separate point in Qdrant.

Suppose there are three records:

DocumentChunk content
Membership refund policyMembers can request a refund within 7 days of purchase
Membership refund policySale items are nonrefundable
Shipping policyStandard orders ship within 48 hours of payment

To replace the entire refund policy, we should delete the first two records and keep the third. How does the application know which chunks belong to the same document?

Assign the same documentId to every chunk stored for a document:

Chunk contentdocumentId
Members can request a refund within 7 days of purchaserefund-policy
Sale items are nonrefundablerefund-policy
Standard orders ship within 48 hours of paymentshipping-policy

documentId is a payload field we define, not a field Qdrant generates automatically. Every chunk still has its own point ID, while chunks from the same document can share a documentId.

To update the refund policy, we can select every record with documentId set to refund-policy, handling all its chunks without deleting the shipping policy.

Let’s verify this in practice.


🛠 Step 1: Store the Old Refund Policy

Reuse Node.js 24.x, openai@7.62.0, @qdrant/js-client-rest@1.20.0, and the existing .env values for AI_API_KEY, AI_BASE_URL, and AI_EMBEDDING_MODEL. Ensure Qdrant is running; no reinstall is needed.

The earlier rag_docs collection did not consistently use documentId. To preserve that lesson data, this article uses a separate collection, rag_maintenance_lesson.

Create 08.seed-maintenance.mjs and define the material:

import OpenAI from "openai";
import { QdrantClient } from "@qdrant/js-client-rest";
import { randomUUID } from "node:crypto";

const ai = new OpenAI({
    apiKey: process.env.AI_API_KEY,
    baseURL: process.env.AI_BASE_URL
});

const qdrant = new QdrantClient({ url: "http://127.0.0.1:6333" });
const collection = "rag_maintenance_lesson";

const documents = [
    { documentId: "refund-policy", text: "Members can request a refund within 7 days of purchase." },
    { documentId: "refund-policy", text: "Sale items are nonrefundable." },
    { documentId: "shipping-policy", text: "Standard orders ship within 48 hours of payment." }
];

There are two refund policy chunks and one shipping policy chunk. The shipping policy lets us check that updating refunds does not delete another document.

Append this to generate vectors and store all three records:

const existing = await qdrant.getCollections();

if (existing.collections.some((item) => item.name === collection)) {
    throw new Error("The exercise collection already exists. Do not rerun the initialization script.");
}

const result = await ai.embeddings.create({
    model: process.env.AI_EMBEDDING_MODEL,
    input: documents.map((item) => item.text)
});

const vectors = result.data.map((item) => item.embedding);

await qdrant.createCollection(collection, {
    vectors: { size: vectors[0].length, distance: "Cosine" }
});

await qdrant.upsert(collection, {
    wait: true,
    points: documents.map((item, index) => ({
        id: randomUUID(),
        vector: vectors[index],
        payload: {
            documentId: item.documentId,
            text: item.text
        }
    }))
});

console.log("Old material prepared: 2 refund policy chunks and 1 shipping policy chunk.");

This reuses embeddings and upsert() without repeating their basic usage. Initialization stops if the collection already exists, preventing duplicate old rules. Run this script only once.

Run:

node --env-file=.env 08.seed-maintenance.mjs

Expected output:

Old material prepared: 2 refund policy chunks and 1 shipping policy chunk.

Qdrant now contains the 7-day refund policy that we need to update.


🔄 Step 2: Change the Refund Window to 14 Days

Create 09.update-maintenance.mjs. This time, define the new version directly:

import OpenAI from "openai";
import { QdrantClient } from "@qdrant/js-client-rest";
import { randomUUID } from "node:crypto";

const ai = new OpenAI({
    apiKey: process.env.AI_API_KEY,
    baseURL: process.env.AI_BASE_URL
});

const qdrant = new QdrantClient({ url: "http://127.0.0.1:6333" });
const collection = "rag_maintenance_lesson";
const documentId = "refund-policy";

const chunks = [
    "Members can request a refund within 14 days of purchase.",
    "Sale items are nonrefundable."
];

Compared with the old version, the first chunk’s refund window has changed from 7 days to 14 days.

Inspect the Old Material First

Append:

const filter = {
    must: [{ key: "documentId", match: { value: documentId } }]
};

async function showChunks(label) {
    const result = await qdrant.scroll(collection, {
        filter,
        limit: 20,
        with_payload: true,
        with_vector: false
    });

    console.log(`\n${label} (${result.points.length} chunks):`);

    for (const point of result.points) {
        console.log("-", point.payload.text);
    }
}

await showChunks("Before update");

scroll() reads records directly using payload conditions. Filtering by documentId shows only refund policy chunks.

On the first update run, you should see the two original passages, including the 7-day refund rule. With only two refund chunks, limit: 20 is sufficient; real documents with more records require pagination.

Delete the Old Chunks and Write the New Ones

Append:

const result = await ai.embeddings.create({
    model: process.env.AI_EMBEDDING_MODEL,
    input: chunks
});

const vectors = result.data.map((item) => item.embedding);

await qdrant.delete(collection, {
    wait: true,
    filter
});

await qdrant.upsert(collection, {
    wait: true,
    points: chunks.map((text, index) => ({
        id: randomUUID(),
        vector: vectors[index],
        payload: { documentId, text }
    }))
});

await showChunks("After update");

This code does two things:

  1. delete() removes all old chunks whose documentId is refund-policy.
  2. upsert() writes the new refund policy’s two chunks.

We generate the new vectors before deleting the old records so an embedding request failure does not clear the existing material prematurely. Deletion and insertion are still separate operations; we will discuss this limitation below.

Run:

node --env-file=.env 09.update-maintenance.mjs

You should see output like this (the two records may appear in a different order):

Before update (2 chunks):
- Members can request a refund within 7 days of purchase.
- Sale items are nonrefundable.

After update (2 chunks):
- Members can request a refund within 14 days of purchase.
- Sale items are nonrefundable.

There are still two chunks after the update, but the old 7-day rule has been replaced by the 14-day rule.


🔍 Step 3: Verify That Other Documents Are Unaffected

The query above shows only the refund policy. We also need to confirm that the shipping policy still exists.

Append this to 09.update-maintenance.mjs:

const shipping = await qdrant.scroll(collection, {
    filter: {
        must: [{ key: "documentId", match: { value: "shipping-policy" } }]
    },
    limit: 20,
    with_payload: true,
    with_vector: false
});

console.log("\nShipping policy:");
for (const point of shipping.points) {
    console.log("-", point.payload.text);
}

Rerun the update script. In addition to the refund policy before and after the update, you should see:

Shipping policy:
- Standard orders ship within 48 hours of payment.

This confirms that the delete condition targets only the refund policy and leaves other documents intact.

Repeated updates should also leave exactly two refund policy chunks, rather than four or six. Each run removes the old records by documentId before storing the current version.

All output above represents expected teaching examples, not test logs from an actual Qdrant connection.


⚠️ Update Considerations

This article uses the straightforward approach of replacing the entire document, but delete() and upsert() are not atomic together. If deletion succeeds and insertion fails, the knowledge base may temporarily lack that document.

This approach is useful for learning the update workflow. Production projects also need to consider failure recovery or version switching, which we will not cover here.

Changes only to payload fields excluded from embedding input generally do not require new vectors. Here, we changed the refund policy text itself, so new embeddings are needed.


❓ Knowledge Check

Q: If another document also mentions “refund,” will it be deleted too?

No. Deletion matches documentId = "refund-policy", not the word “refund” in the text. Only points with that matching field are deleted.


🧪 Exercise

Change “14 days” to “10 days” in 09.update-maintenance.mjs, rerun it, and inspect the Before update and After update sections. Run it again without changes to confirm there are still only two refund chunks and the shipping policy remains.

Success criteria: The updated policy contains only the current 10-day rule, without the old 7-day or 14-day rules. Repeated runs do not accumulate old chunks or delete other documents.


🎉 Congratulations! You can now use documentId to find a document’s chunks, replace old rules with new ones in Qdrant, and verify the update.