@ backloghq/ termlog (英语)
日志结构的全文搜索索引——以LSM收缩为分块的发布列表,BM25排名,零原生依赖.
安装
npm install @backloghq/termlog
使用量
import { TermLog } from "@backloghq/termlog";
const index = await TermLog.open({ dir: "./my-index" });
await index.add("doc-1", "the quick brown fox");
await index.add("doc-2", "the lazy dog");
await index.flush();
const results = await index.search("fox", { limit: 10 });
// [{ docId: "doc-1", score: 0.655... }] (BM25 — exact value depends on corpus)
await index.remove("doc-1");
await index.close();
为什么
现有的FTS引擎(Lucene,Tantivy)需要本土的道具或JVM. 大多数纯-JS替代品将索引序列化为单个的内膜blob——对小蝎子来说是罚款的,但它们在上万个文件中击中了每个文件大小的悬崖. Termlog使用不可改变的盘子上有LSM收缩的片段,所以没有这些上限的活体表.
建筑
- 张贴名单 —— 说
term → [docId, tf],用 VByte / delta 编码压缩. (预留后出 - 术语词典 ——在磁盘上排序;二进制搜索查找.
- 部分 ——自成一体的不可移动文件(term dict + positions). 新写入创建了新段 。 收缩将N段合并为 1.
- 查询执行 布尔(AND/OR),通过张贴标注符(zigzag 为 和,为 OR 进行联合扫描),BM25在上方打分.
- 存储 ——抽象通过
StorageBackend; 默认的本地 FS, S3 通过 @ backloghq/ termlog- s3。 。 。 .
S3 后端
S3支助由配套软件包提供 @ backloghq/ termlog- s3数字 :
npm install @backloghq/termlog @backloghq/termlog-s3
import { TermLog } from "@backloghq/termlog";
import { S3Backend } from "@backloghq/termlog-s3";
import { S3Client } from "@aws-sdk/client-s3";
const index = await TermLog.open({
dir: "my-index",
backend: new S3Backend({
client: new S3Client({ region: "us-east-1" }),
bucket: "my-bucket",
prefix: "my-index/",
}),
});
见 术语- s3 读取器 用于IAM权限、生命周期规则和MinIO/LocalStack的使用.
选项
| 选项 | 默认 | 说明 |
|---|---|---|
fanout | 页:1 | 触发合并的同级线段数( 大小级 LSM) |
flushThreshold | 1 000个 | 自动擦拭前的写入缓冲器 |
k1 | 1.2 国家 | BM25 术语饱和度参数 |
b | 0.75 (简体中文) | BM25 长度规范化参数 |
错误
| 类 | 扔的时候 |
|---|---|
ManifestCorruptionError | 显示. json 包含无效的 JSON |
ManifestVersionError | 显示版本不在所支持的范围之内 |
SegmentCorruptionError | CRC32 错配或缺失段文件 (.region 告诉你什么 |
MappingCorruptionError | docids.snap 或 docids.log 腐败 |
TokenizerMismatchError | 重开使用不同标识符配置的索引 |
IndexLockedError | 另一个过程是咨询 .lock 文件 |
WriteStreamError | 流写失败的基础类( S3 多部分等) |
状态
| 方法 | 回返 | 说明 |
|---|---|---|
docCount() | number | 在所有冲出部分索引的文件 |
segmentCount() | number | 活跃的磁盘片段数 |
estimatedBytes() | number | 近似记忆内足迹(贴出缓冲器+侧车阵列+地图);内存预算调用器的下限估计 |
多写/ S3 安全
术语日志是 每个索引目录的单写器关于当地金融服务的咨询 .lock 文件防止在同一进程组中同时打开。 在S3(或任何共享存储)上,没有分布式锁定——您必须确保每个索引路径最多有一个写入器.
许可证
麻省理工学院