Files
search-test/readme.md
2026-04-10 19:21:27 +08:00

145 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 轻量级搜索引擎对比
本项目对比了三种轻量级全文搜索引擎:**Meilisearch**、**MiniSearch** 和 **FlexSearch**,基于 GitHub Stars 数据集4000+ 条记录)进行测试。
## 功能对比
| 特性 | Meilisearch | MiniSearch | FlexSearch |
|------|-------------|------------|------------|
| **类型** | 服务端(需启动服务) | 内存/浏览器端 | 内存/浏览器端 |
| **中文支持** | 内置分词 | 需自定义分词器 | 需自定义分词器 |
| **向量搜索** | 支持v1.6+ | 不支持 | 不支持 |
| **搜索评分/排名** | 内置 | 内置 | 需 Document 模式 |
| **模糊匹配** | 支持 | 支持 | 支持 |
| **中文分词方案** | cccc/cppej | @node-rs/jieba | @node-rs/jieba |
| **安装体积** | ~20MB + 服务端 | ~100KB | ~50KB |
| **数据源** | starred-repos.json | starred-repos.json | starred-repos.json |
## 搜索效果对比
测试数据:搜索 "资料"
### MiniSearch
- **分词器**@node-rs/jieba
- **结果数量**6 条
- **特点**:部分 token 匹配也算结果(前缀匹配更宽松)
### FlexSearch
- **分词器**@node-rs/jieba
- **结果数量**2 条
- **特点**:需要所有 token 完全匹配
### Meilisearch
- **特点**:需要启动服务,支持更丰富的搜索功能
## 中文分词配置
### MiniSearch + @node-rs/jieba
```typescript
import MiniSearch from 'minisearch'
import { Jieba } from '@node-rs/jieba'
const jieba = new Jieba()
const miniSearch = new MiniSearch({
fields: ['name', 'description', 'author'],
storeFields: ['name', 'description', 'author', 'stars', 'url'],
tokenize: (text) => jieba.cut(text, false), // 使用 jieba 分词
})
miniSearch.addAll(repos)
const results = miniSearch.search('资料')
```
### FlexSearch + @node-rs/jieba
```typescript
import FlexSearch from 'flexsearch'
import { Jieba } from '@node-rs/jieba'
const jieba = new Jieba()
const chineseSegment = (text: string): string[] => {
return jieba.cut(text, false)
}
const index = new FlexSearch.Index({
tokenize: 'full',
resolution: 9,
encode: chineseSegment,
})
repos.forEach((repo) => {
const content = `${repo.name} ${repo.description} ${repo.author}`
index.add(repo.id, content)
})
const results = index.search('资料', { limit: 10 })
```
## 运行示例
```bash
# MiniSearch
bun run minisearch/index.ts
# FlexSearch
bun run flexsearch/index.ts
```
## 适用场景
### 推荐使用 MiniSearch
- 浏览器端搜索
- 小型数据集(< 100k 文档)
- 需要内置评分排名
- 快速集成,无需额外服务
### 推荐使用 FlexSearch
- 浏览器端搜索
- 对包体积敏感
- 需要灵活的索引配置
- 精确匹配场景
### 推荐使用 Meilisearch
- 大型数据集
- 需要服务端部署
- 需要向量搜索
- 需要分布式/高可用
- 丰富的过滤/排序功能
## 局限性
| 搜索引擎 | 局限性 |
|---------|-------|
| **MiniSearch** | 不支持向量搜索,中文需额外配置 |
| **FlexSearch** | 无内置评分Document 模式 API 较复杂 |
| **Meilisearch** | 需要启动服务,增加部署复杂度 |
## 总结
- **轻量级场景**浏览器端、小数据集MiniSearch 或 FlexSearch 足够
- **向量搜索需求**Meilisearch 或引入独立向量数据库
- **混合搜索**:可结合关键词搜索 + 向量搜索,使用 RRF 融合算法
---
## Meilisearch 本地部署
https://www.meilisearch.com/docs/learn/self_hosted/install_meilisearch_locally#windows
```compose.yml
services:
meilisearch:
image: getmeili/meilisearch:v1.15
container_name: meilisearch
ports:
- "7700:7700"
environment:
- MEILI_ENV=development
volumes:
- ./data:/meili_data
restart: unless-stopped
```