本库是一款专为 MoonBit 生态量身定制的、纯 Wasm/JS/Native 兼容、零外部依赖的向量相似度检索与多维空间索引引擎(向量数据库)。针对大模型 RAG 及客户端嵌入式应用进行了优化,支持余弦相似度(Cosine Similarity)、欧氏距离(L2 Distance)、曼哈顿距离(L1 Distance)及点积(Dot Product)等核心度量,并实现了 Flat 线性检索、高
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
MoonVector Similarity Search Library (
moon-vector)A pure MoonBit, zero-dependency high-dimensional Vector Similarity Search and Indexing Engine (Vector Database). It is designed to support client-side and server-side retrieval (Wasm, JS, Native), making it ideal for modern LLM applications such as Retrieval-Augmented Generation (RAG), embedding similarity search, and semantic classification.
MoonVector implements multiple indexing algorithms (Flat, IVF-Flat, KD-Tree, LSH) alongside K-Means clustering, metadata filtering, and JSON serialization.
Features
Documentrecords to/from JSON.IndexStats) such as tree depths, centroid count, and document size.Installation
Add this package to your MoonBit dependencies:
Or manually declare it in your
moon.moddependencies:Quick Start
1. Simple Flat Indexing and Similarity Search
2. High-Dimensional IVF-Flat Cluster Searching
3. Spatial Partitioning with KD-Trees
4. Approximate Cosine Search using LSH
5. Document JSON Serialization
API Reference
1. Distance Metrics
cosine_similarity(v1: Array[Double], v2: Array[Double]) -> Double raise VectorErroreuclidean_distance(v1: Array[Double], v2: Array[Double]) -> Double raise VectorErrormanhattan_distance(v1: Array[Double], v2: Array[Double]) -> Double raise VectorErrordot_product(v1: Array[Double], v2: Array[Double]) -> Double raise VectorError2. Unsupervised Clustering
kmeans(vectors: Array[Array[Double]], k: Int, max_iters: Int) -> Array[Array[Double]] raise VectorError3. Indexing Classes
FlatIndex::new() -> FlatIndexadd(self: FlatIndex, doc: Document) -> Unitsearch(self: FlatIndex, query: Array[Double], top_k: Int, metric: DistanceMetric, filters: Array[(String, String)]) -> Array[SearchResult] raise VectorErrorstats(self: FlatIndex) -> IndexStatsIvfIndex::new(k: Int, metric: DistanceMetric) -> IvfIndexbuild(self: IvfIndex, docs: Array[Document]) -> Unit raise VectorErrorsearch(self: IvfIndex, query: Array[Double], top_k: Int, nprobe: Int, filters: Array[(String, String)]) -> Array[SearchResult] raise VectorErrorstats(self: IvfIndex) -> IndexStatsKdTreeIndex::new(metric: DistanceMetric) -> KdTreeIndexbuild(self: KdTreeIndex, docs: Array[Document]) -> Unitsearch(self: KdTreeIndex, query: Array[Double], top_k: Int, filters: Array[(String, String)]) -> Array[SearchResult] raise VectorErrorstats(self: KdTreeIndex) -> IndexStatsLshIndex::new(num_planes: Int, dim: Int) -> LshIndexadd(self: LshIndex, doc: Document) -> Unit raise VectorErrorsearch(self: LshIndex, query: Array[Double], top_k: Int, filters: Array[(String, String)]) -> Array[SearchResult] raise VectorErrorTesting
Run the full whitebox and blackbox test suite with:
Expected output:
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.