Embedded full-text search engine: inverted index, BM25, Chinese/English tokenization, fuzzy search, did-you-mean, server-side highlighting, segmented index with WAL crash recovery. Runs on native/WASM/JS.
let index = @moonseek.Index::new([
{ name: "title", weight: 3.0 },
{ name: "body", weight: 1.0 },
])
index.add({
id: "doc1",
fields: [
("title", "全文搜索引擎"),
("body", "倒排索引是搜索引擎的核心,BM25 负责排序。"),
],
})
index.add({
id: "doc2",
fields: [("title", "MoonBit"), ("body", "A fast language for WebAssembly")],
})
let hits = index.search("搜索引擎") // [{ id: "doc1", score: … }]
let hits2 = index.search("title:moonbit") // field-scoped
let hits3 = index.search("\"memory safety\" OR wasm*") // phrase + prefix
// Persist (native)
index.save("index.msk")
let restored = @moonseek.Index::load("index.msk")
// Or portable bytes (works on every backend)
let image : Bytes = index.to_bytes()
let restored2 = @moonseek.Index::from_bytes(image)moon run cmd/main -- index ./docs --index index.msk # index .txt/.md files
moon run cmd/main -- search "搜索引擎" --index index.msk
moon run cmd/main -- search "title:rust" --limit 5
moon run cmd/main -- info --index index.mskmoon build --target wasm-gc
cp _build/wasm-gc/debug/build/web/web.wasm demo/web.wasm
cd demo && python -m http.server 8000
# open http://localhost:8000 (Chrome / Edge with WasmGC + js-string builtins)| Operation | Time |
|---|---|
| Index 1000 docs (with stored fields) | ~135 ms |
| Term query (top-10) | ~2.4 ms |
| Phrase query (top-10) | ~1.8 ms |
| Snapshot save+load (with stored fields) | ~75 ms |
token.mbt Analyzer: Latin words + CJK bigrams, positions
index.mbt Inverted index, field stats, BM25, stored fields
query.mbt Lexer → recursive-descent parser → evaluator
fuzzy.mbt Levenshtein/Damerau distance, fuzzy search
suggest.mbt Frequency-weighted did-you-mean
highlight.mbt Server-side highlight/spans/snippet (codepoint offsets)
segment.mbt SegmentedIndex: immutable segments + tiered merge
wal.mbt CRC-framed write-ahead log (portable byte layer)
wal_native.mbt Durable add/checkpoint/crash recovery (native)
codec.mbt Varint / UTF-8 / double / CRC-32 primitives
snapshot.mbt MSK2 format: versioned payload + checksum + stored fields
store.mbt Native atomic file persistence (async/fs)
web/ Browser exports (#export_name, js-string builtins)
cmd/main/ CLI: index / search / info
examples/ Runnable examples (basic, persistence)
demo/ Single-page browser demo (60 docs, benchmarks, inspector)moon test # 179 tests (native), 164 (wasm-gc / js)
moon test --target wasm-gc
moon test --target js
moon bench --release # benchmarks
moon coverage analyze # coverage reportimpl Show for StoreErrorpub struct Index {
schema : Array[FieldSchema]
analyzer : Analyzer
bm25 : Bm25Params
postings : HashMap[String, HashMap[String, Array[Posting]]]
doc_ids : Array[String]
doc_nums : HashMap[String, Int]
stats : HashMap[String, FieldStats]
deleted : HashMap[Int, Unit]
stored : HashMap[Int, Array[(String, String)]]
}fn Index::fuzzy_search(self : Index, term : String, max_distance? : Int, limit? : Int) -> Array[Hit] raise QueryErrorfn Index::fuzzy_terms(self : Index, term : String, max_distance? : Int, field? : String?) -> Array[FuzzyMatch]fn Index::highlight(self : Index, id : String, field : String, query : String, pre_tag? : String, post_tag? : String) -> String raise QueryErrorfn Index::highlight_spans(self : Index, id : String, field : String, query : String) -> Array[HighlightSpan] raise QueryErrorasync fn Index::load_or_create(path : String, schema : Array[FieldSchema]) -> Index raise StoreErrorfn Index::snippet(self : Index, id : String, field : String, query : String, max_chars? : Int, pre_tag? : String, post_tag? : String) -> String raise QueryErrorpub struct Reader {
data : Bytes
pos : Int
}pub struct SegmentedIndex {
schema : Array[FieldSchema]
analyzer : Analyzer
bm25 : Bm25Params
segments : Array[Index]
active : Index
segment_max_docs : Int
}async fn SegmentedIndex::add_durable(self : SegmentedIndex, dir : String, doc : Document) -> Unit raise StoreErrorasync fn SegmentedIndex::delete_durable(self : SegmentedIndex, dir : String, id : String) -> Bool raise StoreErrorasync fn SegmentedIndex::open_or_create(dir : String, schema : Array[FieldSchema]) -> SegmentedIndex raise StoreErrorfn SegmentedIndex::search(self : SegmentedIndex, query : String, limit? : Int) -> Array[Hit] raise QueryErrorfn damerau_levenshtein(a : String, b : String) -> Intfn levenshtein(a : String, b : String) -> Intfn parse_query(input : String, analyzer? : Analyzer, fields? : Array[String]) -> Query raise QueryErrorInstall
Download zipEmbedded full-text search engine: inverted index, BM25, Chinese/English tokenization, fuzzy search, did-you-mean, server-side highlighting, segmented index with WAL crash recovery. Runs on native/WASM/JS.