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

Implementation:0xranx OpenContext NativeIndexer buildIndex

From Leeroopedia
Knowledge Sources
Domains Indexing, Search
Last Updated 2026-10-24 12:00 GMT

Overview

Concrete tool for building, checking, and managing the search index over the OpenContext knowledge base, provided by the native indexer adapter.

Description

The NativeIndexer class wraps the Rust `Indexer` via NAPI bindings. It provides the complete index management API:

  • buildIndex: Full index build — chunks all documents, generates embeddings, stores vectors. Returns stats (file count, chunk count, elapsed time).
  • indexFile: Incremental single-file indexing.
  • removeFile: Remove a file's chunks from the index.
  • indexExists: Check if the LanceDB "chunks" table exists.
  • getStats: Retrieve index statistics (total chunks, last updated).
  • clean: Drop the entire index table, resetting it completely.

The build process goes through three phases: chunking (splitting documents via the Chunker), embedding (batch API calls), and storing (upserting into LanceDB). Progress is reported via a callback.

Note: The NAPI adapter currently does not support progress callbacks from Rust (a TODO exists for ThreadsafeFunction support). A single "done" progress event is emitted after the build completes.

Usage

  • Search Index Management: Build or rebuild the search index via `oc index build`.
  • Index status: Check if an index exists and get statistics via `oc index status`.
  • Index cleanup: Reset the index via `oc index clean`.
  • Background sync: The IndexSyncService uses `indexFile` and `removeFile` for incremental updates.

Code Reference

Source Location

  • Repository: OpenContext
  • File (JS adapter): src/core/search/native-adapter.js
  • File (Rust core): crates/opencontext-core/src/search/indexer.rs
  • File (CLI): bin/oc.js
  • File (MCP): src/mcp/server.js

Signature

class NativeIndexer {
  /**
   * Build index for all documents
   * @param {Object} options
   * @param {Function} options.onProgress - Progress callback
   * @returns {Promise<Object>} Index stats
   */
  async buildIndex(options = {}) { ... }

  /**
   * Index a single file
   * @param {string} relPath - Relative path to the file
   * @returns {Promise<number>} Number of chunks created
   */
  async indexFile(relPath) { ... }

  /**
   * Remove a file from the index
   * @param {string} relPath - Relative path to the file
   */
  async removeFile(relPath) { ... }

  /**
   * Check if index exists
   * @returns {Promise<boolean>}
   */
  async indexExists() { ... }

  /**
   * Get index statistics
   * @returns {Promise<Object>}
   */
  async getStats() { ... }

  /**
   * Clean/reset the index
   */
  async clean() { ... }
}

Import

const { NativeIndexer } = require('./core/search/native-adapter');
// or via facade:
const { Indexer } = require('./core/search');

I/O Contract

Inputs (buildIndex)

Name Type Required Description
options.onProgress Function No Progress callback: ({ phase, percent }) => void

Outputs (buildIndex)

Name Type Description
returns.fileCount number Number of documents indexed
returns.chunkCount number Number of chunks created
returns.mode string Always 'full'
returns.elapsedMs number Build time in milliseconds

Outputs (getStats)

Name Type Description
returns.totalChunks number Total chunks in index
returns.lastUpdated null Last update timestamp (ms)

Usage Examples

Build Index (CLI)

# Full index build
oc index build

# Force rebuild
oc index build --force

# Build for specific folder only
oc index build --folder projects/architecture

Build Index (JavaScript)

const { NativeIndexer } = require('./core/search/native-adapter');

const indexer = new NativeIndexer();

// Build with progress callback
const stats = await indexer.buildIndex({
  onProgress: ({ phase, percent }) => {
    console.log(`Phase: ${phase} (${percent}%)`);
  }
});

console.log(`Indexed ${stats.fileCount} files, ${stats.chunkCount} chunks in ${stats.elapsedMs}ms`);

Check Index Status

const { NativeIndexer } = require('./core/search/native-adapter');

const indexer = new NativeIndexer();
const exists = await indexer.indexExists();

if (exists) {
  const stats = await indexer.getStats();
  console.log(`Index has ${stats.totalChunks} chunks, last updated: ${stats.lastUpdated}`);
} else {
  console.log('No index found. Run `oc index build` to create one.');
}

Clean Index

oc index clean --yes  # Skip confirmation prompt

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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