Implementation:0xranx OpenContext NativeSearcher
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Search, Information_Retrieval |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Concrete tool for executing hybrid, vector, and keyword search over the OpenContext knowledge base, provided by the native search adapter.
Description
The NativeSearcher class wraps the Rust `Searcher` via NAPI bindings, providing a JavaScript-compatible search API. It supports three search modes:
- Hybrid (default): Combines vector and keyword search results using Reciprocal Rank Fusion (RRF) with configurable weights (vector: 0.7, keyword: 0.3).
- Vector: Pure semantic search using embedding similarity.
- Keyword: Pure BM25 keyword matching.
Results can be aggregated by:
- Content: Individual chunks (default).
- Doc: Deduplicated by document, keeping the best chunk per doc.
- Folder: Grouped by parent folder, keeping the best chunk per folder.
The searcher lazily initializes on first use, creating a Rust `Searcher` instance that loads all chunks from the vector store for keyword search.
Usage
- Context Discovery: Search for relevant documents before generating manifests.
- Search Index Management: Execute searches after building the index.
- Agent queries: Used by `oc search` CLI command and `oc_search` MCP tool.
- Result formatting: Results are normalized to snake_case and can be formatted as plain text or JSON via the formatter module.
Code Reference
Source Location
- Repository: OpenContext
- File (JS adapter): src/core/search/native-adapter.js
- File (Rust core): crates/opencontext-core/src/search/searcher.rs
- File (CLI): bin/oc.js
- File (MCP): src/mcp/server.js
- File (formatter): src/core/search/formatter.js
Signature
class NativeSearcher {
constructor(options = {}) {
this.vectorWeight = options.vectorWeight ?? 0.7;
this.keywordWeight = options.keywordWeight ?? 0.3;
this.initialized = false;
this._searcher = null;
}
/**
* Execute search
* @param {string} query - Search query
* @param {Object} options
* @param {number} options.limit - Max results (default 5)
* @param {string} options.mode - 'hybrid' | 'vector' | 'keyword' (default 'hybrid')
* @param {string} options.aggregateBy - 'content' | 'doc' | 'folder' (default 'content')
* @param {string} options.docType - 'doc' | 'idea' (optional filter)
* @returns {Promise<Array<SearchHit>>} Normalized search results
*/
async search(query, options = {}) { ... }
async initialize(forceReinit = false) { ... }
formatResults(query, results, options) { ... }
formatResultsJson(query, results, options) { ... }
}
Import
const { NativeSearcher } = require('./core/search/native-adapter');
// or via facade:
const { Searcher } = require('./core/search');
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| query | string | Yes | Search query text |
| options.limit | number | No | Max results to return (default 5) |
| options.mode | string | No | Search mode: 'hybrid', 'vector', or 'keyword' (default 'hybrid') |
| options.aggregateBy | string | No | Aggregation: 'content', 'doc', or 'folder' (default 'content') |
| options.docType | string | No | Filter by type: 'doc' or 'idea' |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | Promise<Array<SearchHit>> | Normalized search results |
| SearchHit.score | number | Relevance score |
| SearchHit.file_path | string | Document file path |
| SearchHit.content | string | Chunk content |
| SearchHit.heading_path | string | Heading hierarchy path |
| SearchHit.line_start | number | Start line number |
| SearchHit.line_end | number | End line number |
| SearchHit.matched_by | string | Match type: 'vector', 'keyword', or 'hybrid' |
| SearchHit.display_name | string | Document display name |
| SearchHit.doc_type | string | 'doc' or 'idea' |
Usage Examples
Basic Hybrid Search (CLI)
oc search "architecture decisions" --format json --limit 10
Vector-Only Search (JavaScript)
const { NativeSearcher } = require('./core/search/native-adapter');
const searcher = new NativeSearcher();
const results = await searcher.search('how to load models', {
limit: 5,
mode: 'vector',
aggregateBy: 'doc'
});
results.forEach(hit => {
console.log(`[${hit.score.toFixed(3)}] ${hit.file_path} (${hit.heading_path})`);
console.log(hit.content.substring(0, 100));
});
Keyword Search with Doc Aggregation
const { NativeSearcher } = require('./core/search/native-adapter');
const searcher = new NativeSearcher();
const results = await searcher.search('FastLanguageModel', {
limit: 10,
mode: 'keyword',
aggregateBy: 'folder'
});
// One best result per folder
results.forEach(hit => {
console.log(`${hit.file_path}: ${hit.heading_path}`);
});
MCP Tool Usage
{
"tool": "oc_search",
"arguments": {
"query": "model loading",
"limit": 10,
"mode": "hybrid",
"aggregateBy": "content"
}
}
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment