---
title: Catalog Search Index Maintenance
summary: Maintaining the derived semantic search index after catalog changes.
owners:
  - catalog-experience-team
badges:
  - content: Runtime Scenario
    backgroundColor: purple
    textColor: purple
---

Catalog API records revisions to authoritative book data, while scheduled work refreshes the derived search index in bounded batches. The search index may lag catalog changes but must never overwrite a newer revision.

## Purpose & Trigger

The flow keeps the semantic search index aligned with book creations, updates, and removals. It starts from a catalog data change or a scheduled batch trigger.

## Visual References

_Catalog Search Index Refresh_

<MermaidFileLoader file="mermaids/002-catalog-search-index-maintenance-diagram-1.mmd" />

## Step-by-Step Walkthrough

1. Catalog API commits a book change synchronously and records a pending revision for indexing.
2. PostgreSQL stores the pending work with the latest revision for the affected book.
3. Scheduler periodically publishes an asynchronous batch trigger to Kafka.
4. Catalog Ingestion receives the trigger and attempts exclusive access to the catalog batch.
5. If another run owns the batch or no work is pending, the run exits without changing data.
6. Otherwise, Catalog Ingestion reads at most 200 revisions and synchronously requests embeddings from Microsoft Foundry for active books.
7. Catalog Ingestion upserts active vectors and removes vectors for deleted or missing books in Qdrant.
8. It acknowledges a revision only when it still matches the revision processed, preserving later edits for the next batch.

## Error Handling & Edge Cases

- Lock contention and an empty queue are successful no-op outcomes.
- An embedding or search-index failure leaves pending revisions unchanged and releases exclusive access.
- The acknowledgement is revision-safe, so an edit made during processing is not lost.
- Kafka redelivery may start another batch, but the exclusive lock prevents overlapping processing.

## Quality & Consistency Notes

- Catalog state is authoritative; Qdrant is derived and may lag by up to one scheduled interval plus processing time.
- Batch size and scheduling bound the amount of concurrent embedding and search-index work.
- Revision matching provides eventual consistency without overwriting a newer catalog change.
