static_search 教程
本教程介绍如何构建并提供静态站点、它的搜索如何回答查询,以及如何用 static_search 包在 MoonBit 中运行同一个搜索引擎。static_search 设计详细解释了索引和排序。
| 我想要 | 使用 |
|---|---|
| 构建并提供静态站点 | npm run build:static-data, npm run build:static, npm run serve:static |
| 查找包 | 原生表达式语言,例如 json AND score>=180 |
| 预测结果的顺序 | 相关度(匹配的项数),然后是名次,然后是名称 |
| 在 MoonBit 中搜索索引 | @static_search.parse_index, @static_search.search_items |
| 用一个查询检验一个包 | @static_search.evaluate, @static_search.relevance |
快速开始
添加模块并导入这两个包:
moon add Luna-Flow/mooncake-impact-factor@0.2.0
import {
"Luna-Flow/mooncake-impact-factor/query",
"Luna-Flow/mooncake-impact-factor/static_search",
}
搜索一个小索引:
test "quick start" {
let index =
#|{"items":[
#| {"full_name":"alice/json","rank_label":"A","momentum_label":"Rising","score":200,
#| "score_growth_30d":5,"download_count":10,"dependent_count":3,"recent_dependent_count":1,
#| "external_dependent_count":2,"dependent_owner_count":2,"days_since_release":12,"rank_position":1,
#| "latest_created_at":"2026-01-01T00:00:00Z","repository_present":true,"license_present":true,
#| "normalized_full_text":"alice/json alice json json parser","normalized_owner":"alice",
#| "normalized_package":"json","normalized_description":"json parser","normalized_license":"mit",
#| "normalized_repository":"","normalized_keywords":["json"]},
#| {"full_name":"bob/csv","rank_label":"C","momentum_label":"New","score":60,
#| "score_growth_30d":0,"download_count":0,"dependent_count":0,"recent_dependent_count":0,
#| "external_dependent_count":0,"dependent_owner_count":0,"days_since_release":400,"rank_position":2,
#| "latest_created_at":null,"repository_present":false,"license_present":false,
#| "normalized_full_text":"bob/csv bob csv csv reader","normalized_owner":"bob",
#| "normalized_package":"csv","normalized_description":"csv reader","normalized_license":"",
#| "normalized_repository":"","normalized_keywords":[]}
#|]}
let items = @static_search.parse_index(index)
let result = @static_search.search_items(items, { "q": "JSON" })
inspect(result.total, content="1")
inspect(items[result.indices[0]].full_name, content="alice/json")
}
参数就是 Web 界面的参数;表达式语言见 query 教程。该包可为所有目标构建,因此 moon test --target all 会在每个后端上运行它。
日常任务
构建并提供静态站点
在仓库根目录下:
npm install
npm run build:static-data # builds cli, the database and public/data
npm run build:static # builds the MoonBit modules, then next build into out/
npm run serve:static # serves out/ on http://localhost:4173
build:static-data 需要 ~/.moon/registry/index/user 下的本地注册表索引(先运行 moon update)。搜索索引是 public/data/search/search-index.json;浏览器会把它一次性加载到 Web Worker 中,由后者运行编译为 JavaScript 的 MoonBit 引擎。
编写查询
| 查询 | 查找结果 |
|---|---|
json | 名称、所有者、描述或关键词中包含 json 的包 |
"http client" | 子串 http client,包括其中的空格 |
owner:moonbitlang AND keyword:json | 该所有者的、关键词包含 json 的包 |
(yaml OR toml) AND NOT rank=D | 等级不是 D 的 YAML 或 TOML 包 |
momentum=rising AND recent_dependents>=5 | 至少有五个近期依赖方的快速增长的包 |
age<=30 AND owners>=2 | 在最近 30 天内发布、且被至少两个其他所有者使用的包 |
界面的扁平筛选条件同样可用:URL 中的 rank=S,A 保留等级 S 和 A,limit 和 offset 用于在结果中翻页。
理解结果的顺序
没有显式排序时,有查询的结果按一个包匹配的正项数排序,然后按名次,然后按名称;没有查询时,列表按名次排序。有显式排序时,order 翻转排序键,而并列项总是按名次从好到差列出。
test "order of results" {
fn item(name : String, text : String, score : Double) -> @static_search.IndexItem {
{
full_name: name,
rank_label: "C",
momentum_label: "Stable",
score,
score_growth_30d: 0.0,
download_count: 0.0,
dependent_count: 0.0,
recent_dependent_count: 0.0,
external_dependent_count: 0.0,
dependent_owner_count: 0.0,
days_since_release: 0.0,
rank_position: 0.0,
latest_created_at: None,
repository_present: false,
license_present: false,
normalized_full_text: @static_search.normalize_text("\{name} \{text}"),
normalized_owner: "",
normalized_package: "",
normalized_description: "",
normalized_license: "",
normalized_repository: "",
normalized_keywords: [],
}
}
let items = [
item("alice/json", "Fast JSON parser", 120.0),
item("bob/toml", "TOML parser", 300.0),
item("carol/yaml", "YAML and JSON emitter", 80.0),
]
let result = @static_search.search_items(items, { "expr": "json OR parser" })
debug_inspect(result.indices.map(i => items[i].full_name), content=(
#|["alice/json", "bob/toml", "carol/yaml"]
))
}
alice/json 匹配两个词;另外两个各匹配一个,按名次排序(这里所有名次都是 0,因此按名称排序)。用 json AND parser 时只有 alice/json 匹配。相关度相同的结果总是先按名次、再按名称排序,与排序方向无关。
在 JavaScript 中使用该引擎
npm run build:moonbit 写出 lib/moonbit/static_search/static_search.js。lib/static-search.ts 包装它:
import { loadStaticSearchIndex, searchStaticIndex } from "../lib/static-search";
loadStaticSearchIndex(indexText); // once
const page = searchStaticIndex({ q: "json", limit: "20" });
page.indices; // positions in the index
page.total; // matches before paging
陷阱
- 隐式 AND。 在表达式语言中,不带
AND的json parser是语法错误。在简单搜索字段(q)中,同样的文本是一个子串json parser。 - 字段运算符。
score:180表示score = 180,而不是至少 180。请使用score>=180。 NOT不会提高相关度。 只有正词项计数,因此对NOT rank=D OR json,每个提到json的包都排在其余包之前,彼此之间按分数排序,与等级无关。- 索引字段。 在评分 v2 之前导出的索引缺少
rank_position和其他新增的计数;load_index会拒绝它。请重新导出数据。
下一步
- static_search 设计:索引布局、相关度计数、排序规则以及查询的开销。
- static_search API:每个函数和 JavaScript 导出项。
- query 教程:表达式语言。
- 架构指南:完整的数据管道。