npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dsh-fulltext-index

v0.1.0

Published

In-memory full-text search plugin for DeepSeek Harness: inverted index with deterministic tokenization (ASCII words lowercased, CJK runs as character bigrams with singleton unigram fallback), TF-IDF ranked search with title field boosting (2x), match cove

Readme

dsh-fulltext-index

面向 DeepSeek Harness 生态的纯内存全文检索插件(零运行时依赖):倒排索引、CJK bigram 分词、TF-IDF 相关性排序、单文档打分解释。不碰磁盘、不联网、无状态(每次调用独立建索引)。

  • 源码:https://github.com/geeklei/dsh-plugins/tree/main/dsh-fulltext-index
  • npm:https://www.npmjs.com/package/dsh-fulltext-index
  • 协议:MIT

功能说明

| 工具 | 作用 | 返回 | |---|---|---| | build_index | 构建倒排索引 | 每个 token 的 df、各文档词频与出现位置(正文/标题分开计数) | | search | TF-IDF 相关性检索 | 排名结果(得分/覆盖度/命中与缺失词/命中片段) | | explain_match | 单文档打分解释 | 逐查询词的 tf/titleTf/有效词频/df/idf/贡献度分解 |

分词口径(确定性,中英文同构):

  • ASCII 词:连续 [A-Za-z0-9_] 一段,小写化("Hello World" → hello/world)
  • CJK 汉字:连续汉字串做 bigram("深圳天气" → 深圳/圳天/天气);孤立单字保留 unigram("深" → 深)
  • 其余字符一律视为分隔符;全角字母/假名等不在处理范围(防误命中)
  • 所有 token 保留原文位置,供命中片段定位

评分口径:

  • 有效词频 = 正文词频 + 标题词频 × 2(标题是文档的强信号)
  • idf = ln(1 + N/df)(平滑 idf,df 越小权重越高,df=N 时仍有非零权重)
  • score = Σ 有效词频 × idf;按分数降序,同分保持文档原序(稳定排序)
  • 覆盖度 = 命中的查询词数 / 查询词总数(去重后)

安装

# 从 npm 安装
dsh plugin --profile web add dsh-fulltext-index
# 本地开发调试(Debug patch):新建 debug.patch.yml
# - insert:
#     - id: fulltext-index
#       name: "E:/dsh-plugins/dsh-fulltext-index/index.js"

使用说明

1. build_index —— 构建倒排索引

| 参数 | 类型 | 说明 | |---|---|---| | docs | array | 文档数组,元素 {id, text, title?};id 缺省自动 doc1..docN;重复 id 报错 |

// 输入 [{id:"a", text:"深圳天气晴,深圳空气好", title:"深圳日报"}] 输出(节选)
{
  "docCount": 1,
  "tokenCount": 9,
  "totalTokens": 11,
  "terms": [
    { "token": "深圳", "df": 1, "postings": [
      { "doc": "a", "tf": 2, "titleTf": 1, "positions": [0, 6], "titlePositions": [0] }
    ]}
  ]
}

2. search —— TF-IDF 检索

| 参数 | 类型 | 默认 | 说明 | |---|---|---|---| | docs | array | 必填 | 文档数组(同上) | | query | string | 必填 | 查询串,与文档同口径切词 | | top_k | number | 10 | 返回前 K 条(钳制到 1~100) |

返回 { query, tokens, docCount, topK, hitCount, results };每个结果含 score(6 位小数)、coverage、matched/missing(逐查询词)、snippet(命中位置前 15 后 25 字符,超界加省略号;仅标题命中时取标题)。无命中返回空结果不报错。

3. explain_match —— 打分解释

参数同 search,另加 doc_id(必填,须在 docs 中存在)。对每个查询词给出贡献度分解,并列出未命中词与覆盖度——用于理解排序依据、调优文档措辞或验证分词口径。

典型场景

  1. 本地知识库检索:把若干 Markdown/文本文档喂给 search,按相关性拿回 id + 命中片段,再让 Agent 精读命中的原文
  2. 先解释再调优:explain_match 看某篇文档为什么排不上——通常是标题没带关键词(标题权重 ×2)或术语被 bigram 切碎
  3. 验证分词口径:build_index 直接检视 token 流与词频分布,确认 bigram 边界符合预期(如产品名"深智城"会切成"深智/智城"两个 bigram)
  4. 与 dsh-file-search 分工:file-search 是 grep 式逐文件内容检索(找"哪些文件包含这个词");本插件是多文档相关性排序检索(找"哪篇文档最相关")
  5. 为 mindvault v0.4 打底:同一套倒排索引 + CJK bigram + 命中片段方案将复用到 mem_search

安全边界

  • 纯内存无状态:不读写文件、不联网;每次调用独立建索引,无跨调用缓存
  • 不改输入:docs 原对象不被修改
  • 确定性:同样的输入永远得到同样的分词与得分(无随机、无浮点噪声积累,得分四舍五入到 6 位小数)
  • 规模提示:O(总 token 数),万行级文档无压力;索引不落盘,超大文档集建议分块多次调用
  • 不做的:无模糊匹配/同义词/拼音检索(RoadMap);无语义向量;无停用词默认表(可用分词后的 tokens 自行过滤)

版本说明

  • 0.1.0(2026-10-24):首版。build_index / search / explain_match 三工具;91 测试断言(真实 dsh-tools 包装层环境全绿)
  • 已知边界:CJK 只覆盖 U+3400-U+9FFF(不含假名/谚文/全角字母);top_k 钳制 1~100;无停用词

RoadMap

  • v0.2:字段权重可配置(title_weight 参数化);停用词内置表(中英各 ~30 词,可关);highlight 参数让片段中命中词加标记
  • v0.3:模糊匹配(CJK 单字容错:bigram 未命中时降级 unigram 匹配);拼音首字母检索(零依赖音节表)
  • v0.4:持久索引(索引 JSON 落盘 + 增量更新);BM25 评分可选(k1/b 参数)

其他插件集成说明

  • dsh-file-search:互补分工——file-search 找"哪些文件包含",本插件答"哪篇最相关"
  • dsh-mindvault:v0.4 mem_search 将复用本插件的分词与评分口径(bigram + 平滑 idf),vault 页面即 docs
  • dsh-markdown-render / dsh-html-toolkit:先用它们把 md/html 抽成纯文本,再喂 docs
  • dsh-data-clean:文档表格先清洗(去空 token 行)再建索引
  • dsh-stats-toolkit:对 search 的得分列跑 describe_stats,检视查询质量的得分分布

FAQ

Q1:为什么"深智城"搜不出来? 它会被切成"深智/智城"两个 bigram——这是 bigram 方案的固有行为。查"深智城"作为整串查询时同样被切成这两个词,所以能正常命中含"深智城"的文档;但查"智城"(文档里没有相邻的"智城")不会命中。可查 RoadMap v0.3 的降级匹配。

Q2:标题权重为什么是 ×2? 标题是人工提炼的强信号(页名/文件名/标题行)。×2 是 v0.1 的固定值,v0.2 会参数化。

Q3:idf = ln(1 + N/df),为什么不取负对数? 标准 idf = ln(N/df) 在 df=N 时为 0(常见词无区分度),但在小文档集上过于激进;平滑版 ln(1+N/df) 保证常见词仍有小的正贡献,小集合下排序更稳。

Q4:无命中为什么返回空而不报错? "没搜到"是正常业务结果,报错会中断 Agent 流程;调用方看 hitCount === 0 即可。

Q5:同一 query 两次调用得分一致吗? 一致。tokenize 与评分全确定性,无随机性;得分统一 6 位小数避免浮点尾巴。

Q6:能索引多少文档? 受内存限制,单次调用数万文档(每篇几 KB)无压力。索引不持久化,重复查询会重复建索引——v0.4 会加落盘缓存。

许可证

MIT