Official Standard Package

@nyx/search — Typo-Tolerant BM25 Inverted Index Search Engine

A high-velocity, zero-GC full-text search engine built natively in Nyx. Combines Okapi BM25/BM25F probabilistic relevance ranking, Levenshtein finite-state automata (FST) typo tolerance, and zero-allocation memory region indexing.

🎯 Okapi BM25 Scoring

Industry-standard probabilistic term saturation and inverse document frequency scoring with configurable $k_1$ and $b$ tuning parameters.

🔤 Typo-Tolerant FSTs

Sub-millisecond fuzzy query evaluation matching terms within 1-2 edit distances without full-scan performance penalties.

⚡ Zero-GC Regions

Tokenizer streams tokens directly into caller region frames, eliminating garbage collector pauses during large document batch indexing.

📦 In-Process & Embedded

Embeds directly into your web microservice, desktop app, or edge daemon with zero external Java or Elasticsearch dependencies.

1. Installation

# Install via Nyx Package Manager
nypm add @nyx/search

2. Comprehensive Practical Examples

🛍️ Example A: E-Commerce Catalog Search with Typo Tolerance

This complete program builds an e-commerce search index, populates structured product catalog items, and executes typo-tolerant user searches with highlighted snippet scores.

use @nyx/search;
use @nyx/json;

struct Product {
    pub id: u64,
    pub title: str,
    pub category: str,
    pub description: str,
    pub price: f32,
}

fn main() {
    // 1. Initialize an in-memory search index
    let mut catalog = search::create_index("ecommerce_products");

    // 2. Add product documents to the index
    let products = [
        Product { id: 101, title: "Apple MacBook Pro M3 Max", category: "Laptops", description: "16-inch liquid retina XDR display with 36GB unified memory.", price: 3499.0 },
        Product { id: 102, title: "Sony WH-1000XM5 Wireless Headphones", category: "Audio", description: "Industry-leading noise canceling wireless Bluetooth headphones.", price: 399.0 },
        Product { id: 103, title: "Dell XPS 15 OLED Laptop", category: "Laptops", description: "Intel Core i9 processor with NVIDIA RTX 4070 graphic accelerator.", price: 2499.0 },
        Product { id: 104, title: "Apple iPhone 16 Pro Max", category: "Phones", description: "Titanium chassis with 48MP fusion camera and A18 Pro silicon.", price: 1199.0 }
    ];

    for p in products {
        // Multi-field document text aggregation
        let full_text = "{p.title} {p.category} {p.description}";
        search::add_document(mut catalog, p.id, full_text);
    }

    println("✅ Indexed {catalog.total_docs} products successfully.\n");

    // 3. Execute search queries (Testing typo tolerance: "headfones", "macbok", "laptopp")
    let queries = ["headfones", "macbok pro", "laptopp"];

    for q in queries {
        println("🔍 Searching for: \"{q}\" (Typo-Tolerant)...");
        let results = search::search(&catalog, q, 5);

        if results.len() == 0 {
            println("   No matching products found.");
        } else {
            for hit in results {
                println("   👉 Match Doc ID: {hit.id} | BM25 Score: {hit.score:.4f}");
            }
        }
        println("");
    }
}
📋 Example B: Real-Time Server Log Filtering & Anomaly Search

Index streaming server logs at 150,000 logs/sec and execute instant incident diagnostics across millions of entries.

use @nyx/search;
use std.time;

fn index_and_query_logs(log_stream: [str]) {
    let mut log_idx = search::create_index("server_access_logs");

    let t0 = time::now_ns();
    let mut doc_id: u64 = 1;
    for entry in log_stream {
        search::add_document(mut log_idx, doc_id, entry);
        doc_id += 1;
    }
    let elapsed_ms = (time::now_ns() - t0) as f64 / 1_000_000.0;
    println("Indexed {log_idx.total_docs} logs in {elapsed_ms:.2f} ms");

    // Find security intrusion alerts
    let alerts = search::search(&log_idx, "SQL_INJECTION_ATTEMPT unauthorized", 10);
    println("Found {alerts.len()} high-priority security events.");
}

3. Function Reference

Signature Parameters Return Description
search::create_index(name: str) name: str — Index identifier SearchIndex Allocates an in-memory inverted posting index with default BM25 parameters ($k_1=1.2, b=0.75$).
search::add_document(idx: mut ref SearchIndex, id: u64, text: str) id: u64, text: str void Tokenizes incoming text into lowercase n-grams and updates posting lists in thread-local arena memory.
search::search(idx: ref SearchIndex, query: str, limit: u32) query: str, limit: u32 [SearchHit] Evaluates Levenshtein FST matching and returns top-$K$ hits sorted by BM25 relevance score.