Embedded full-text search engine: inverted index, BM25, Chinese/English tokenization, fuzzy search (BK-tree), did-you-mean, server-side highlighting, faceted filtering, score explanation, 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 |
| Operation | Time |
|---|---|
| Index build | ~6.7 s (≈3,000 docs/s) |
| Selective term query (df ≈ 300) | ~0.02 ms |
| Prefix query | ~17 ms (dominated by scoring the matched terms) |
| Saturated term (matches every doc) | ~13 ms |
| Phrase query | ~18 ms |
| Facet filter + aggregation (3k matches) | ~21 ms |
| Fuzzy lookup, 24k terms | linear ~100 ms → BK-tree 0.05–12 ms |
| Snapshot 17 MB: serialize / load | ~2.4 s / ~1.7 s |
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)
facet.mbt Facet values, filters, aggregations, pagination
bktree.mbt BK-tree for sub-linear fuzzy lookup
explain.mbt BM25 breakdown, sort orders, cursor pagination
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 MSK3 format: versioned payload + checksum + stored fields + facets
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 # 217 tests (native), 203 (wasm-gc / js)
moon test --target wasm-gc
moon test --target js
moon bench --release # benchmarks
moon coverage analyze # coverage report
moon run bench/stress --target native --release # 20k-doc stress benchmarkimpl Show for StoreErrorpub struct BkTree {
// private fields
}pub(all) struct Explanation {
id : String
total_score : Double
details : Array[ScoreDetail]
} derive(Eq, Debug)pub 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)]]
facets : HashMap[Int, Array[(String, Array[String])]]
terms_sorted : HashMap[String, Array[String]]
bk_trees : HashMap[String, BkTree]
}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::fuzzy_terms_fast(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::search_after(self : Index, query : String, after_score~ : Double, after_id~ : String, limit? : Int) -> Array[Hit] raise QueryErrorfn Index::search_faceted(self : Index, query : String, options? : SearchOptions) -> FacetedResult raise QueryErrorfn 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(all) struct SearchOptions {
limit : Int
offset : Int
filters : Array[FacetFilter]
facet_fields : Array[String]
} derive(Eq, Debug)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 damerau_levenshtein_bounded(a : String, b : String, bound : Int) -> 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 (BK-tree), did-you-mean, server-side highlighting, faceted filtering, score explanation, segmented index with WAL crash recovery. Runs on native/WASM/JS.