Principle:0xranx OpenContext Index Build
| Knowledge Sources | |
|---|---|
| Domains | Indexing, Information_Retrieval |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
A pipeline principle for building a search index by chunking documents, generating embeddings, and storing vectors with metadata, with progress tracking and dimension auto-detection.
Description
Index building is the process of transforming a corpus of markdown documents into a searchable vector index. It is the prerequisite for semantic search and involves a multi-stage pipeline:
- Document Collection: Fetch all documents from the SQLite store, including their abs_path and rel_path.
- Chunking: Split each document into semantic chunks using heading-aware chunking (1500 chars max, 200 chars overlap).
- Idea Entry Parsing: For idea files (under `.ideas/`), parse individual entries as separate chunks, each with its own `entry_id` and `idea_box` metadata.
- Embedding Generation: Batch all chunk texts and send them to the embedding API in configurable batch sizes.
- Dimension Verification: After the first batch, detect the actual embedding dimension from the API response. If it differs from the configured dimension, re-initialize the vector store with the correct dimension.
- Vector Storage: Upsert all chunks (content + vector + metadata) into the LanceDB table.
- Progress Reporting: Emit progress events at each phase (chunking, embedding, storing, done) with current/total counts and percentage.
The build can be full (re-index all documents) or incremental (index only specific files). A full rebuild can be forced with the `force` flag.
Usage
Use this principle when initializing a search index for the first time or when a complete rebuild is needed. Be aware that index builds incur external embedding API costs proportional to the corpus size. The build should be a controlled operation, not auto-triggered by agents.
Theoretical Basis
The index build pipeline:
Pseudo-code Logic:
# Abstract algorithm (NOT real implementation)
def build_index(docs, config, on_progress):
chunker = Chunker(max_chars=1500, overlap=200)
embedding_client = EmbeddingClient(config.embedding)
vector_store = VectorStore(config.lancedb_path, config.embedding.dimensions)
# Phase 1: Chunking
all_chunks = []
for i, doc in enumerate(docs):
chunks = chunker.chunk(doc.content, doc.rel_path)
# Parse idea entries if applicable
if doc.rel_path.startswith(".ideas/"):
chunks = parse_idea_entries(doc.content, chunks)
all_chunks.extend(chunks)
on_progress(phase="chunking", current=i+1, total=len(docs))
# Phase 2: Embedding (batched)
texts = [chunk.content for chunk in all_chunks]
for batch in batched(texts, config.embedding.batch_size):
vectors = embedding_client.embed(batch)
if not dimensions_verified:
actual_dim = len(vectors[0])
if actual_dim != config.embedding.dimensions:
vector_store = VectorStore(config.lancedb_path, actual_dim)
dimensions_verified = True
on_progress(phase="embedding", current=processed, total=len(texts))
# Phase 3: Storage
vector_store.upsert(all_chunks_with_vectors)
on_progress(phase="storing", current=len(all_chunks), total=len(all_chunks))
return IndexStats(total_docs=len(docs), total_chunks=len(all_chunks))
Practical Guide
- Cost awareness: Index builds call external embedding APIs; cost scales with corpus size. Do not auto-trigger.
- Progress tracking: Use the progress callback to show phase, current/total, and percentage to users.
- Dimension handling: The system auto-detects embedding dimensions from the first API response and re-initializes the vector store if needed.
- Incremental updates: For single-file updates, use `indexFile()` which removes old chunks for that file and inserts new ones.
- Idea entries: Files under `.ideas/` are parsed into individual entries, each indexed separately with `doc_type: "idea"`.
Related Pages
Implemented By
- Implementation:0xranx_OpenContext_NativeIndexer_buildIndex
- Implementation:0xranx_OpenContext_Chunker
- Implementation:0xranx_OpenContext_EmbeddingClient
- Implementation:0xranx_OpenContext_VectorStore