Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xranx OpenContext Index Build

From Leeroopedia
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:

  1. Document Collection: Fetch all documents from the SQLite store, including their abs_path and rel_path.
  2. Chunking: Split each document into semantic chunks using heading-aware chunking (1500 chars max, 200 chars overlap).
  3. Idea Entry Parsing: For idea files (under `.ideas/`), parse individual entries as separate chunks, each with its own `entry_id` and `idea_box` metadata.
  4. Embedding Generation: Batch all chunk texts and send them to the embedding API in configurable batch sizes.
  5. 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.
  6. Vector Storage: Upsert all chunks (content + vector + metadata) into the LanceDB table.
  7. 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:

Docs→chunkChunks→embedVectors→storeIndex

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

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment