static_search API

用途

包 Luna-Flow/mooncake-impact-factor/static_search 是静态站点的搜索引擎。它读取导出的搜索索引,用 query 树对其中的记录求值,计算相关度,按静态站点的规则排序并切分页面。浏览器通过生成的 JavaScript 模块在 Web Worker 中运行它;MoonBit 代码可以直接调用它。

本包是纯 MoonBit 代码,可为所有目标构建。文本的小写转换和名称的排序规则都由本包自己实现,因此结果不依赖于宿主的 toLowerCase 或 localeCompare。

源码:src/static_search。static_search 设计推导了排序方式及其开销。

导入

import {
  "Luna-Flow/mooncake-impact-factor/query",
  "Luna-Flow/mooncake-impact-factor/static_search",
}

索引记录

pub(all) struct IndexItem {
  full_name : String
  rank_label : String
  momentum_label : String
  score : Double
  score_growth_30d : Double
  download_count : Double
  dependent_count : Double
  recent_dependent_count : Double
  external_dependent_count : Double
  dependent_owner_count : Double
  days_since_release : Double
  rank_position : Double
  latest_created_at : String?
  repository_present : Bool
  license_present : Bool
  normalized_full_text : String
  normalized_owner : String
  normalized_package : String
  normalized_description : String
  normalized_license : String
  normalized_repository : String
  normalized_keywords : Array[String]
}
pub fn IndexItem::from_json(Json) -> Self raise IndexError
pub fn parse_index(String) -> Array[IndexItem] raise IndexError

pub suberror IndexError {
  IndexError(String)
}
pub fn IndexError::message(Self) -> String

search/search-index.json 中的一条记录,只包含搜索所需的部分;normalized_* 文本已经是小写。from_json 读取一条记录并忽略未知成员;成员缺失或类型错误时抛出 IndexError("Index item field must be …")。parse_index 读取文档 {"items": [ … ]} 或裸数组。

对查询求值

pub fn match_term(IndexItem, @query.Term) -> Bool
pub fn evaluate(IndexItem, @query.Node) -> Bool
pub fn relevance(IndexItem, @query.Group) -> Int
pub fn package_year(IndexItem) -> Double

match_term 检验一个项,忽略其取反:

字段成立条件
text, owner, package, description, license, repository去除首尾空白并转为小写的值是记录文本的子串(空白值总是成立)
keyword同样的检验对某个关键词成立
rank, momentum标签等于用 @query.parse_label 读取的值(忽略大小写和两侧空格);不是标签的值按原样比较
score, dependents, recent_dependents, external_dependents, owners, downloads, year, age, position数值与 @query.number_value(value) 比较:>=、<=,= 和 : 都按 = 处理;值不是有限数时从不成立
has_repository, has_license标志等于 value == "true"

evaluate 应用取反和连接词(空 And 组成立,空 Or 组不成立)。relevance 统计记录所匹配的正项叶子数;位于奇数个 NOT 之下的叶子计为零。package_year 读取 latest_created_at 的前四个字符(缺失或不是数字时为 0)。

test "relevance ignores negated leaves" {
  let ast = @query.parse_expression("NOT rank=D OR json")
  let item : @static_search.IndexItem = {
    full_name: "dave/jsond",
    rank_label: "D",
    momentum_label: "Stable",
    score: 10.0,
    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: 40.0,
    rank_position: 3.0,
    latest_created_at: Some("2026-01-01T00:00:00Z"),
    repository_present: false,
    license_present: false,
    normalized_full_text: "dave/jsond dave jsond json parser",
    normalized_owner: "dave",
    normalized_package: "jsond",
    normalized_description: "json parser",
    normalized_license: "",
    normalized_repository: "",
    normalized_keywords: [],
  }
  inspect(@static_search.evaluate(item, Group(ast)), content="true")
  inspect(@static_search.relevance(item, ast), content="1")
  inspect(@static_search.package_year(item), content="2026")
}

搜索

pub(all) struct SearchResult {
  indices : Array[Int]
  total : Int
}
pub fn search_items(Array[IndexItem], Map[String, String]) -> SearchResult raise @query.QueryError
pub fn sort_indices(Array[IndexItem], Array[Int], @query.Group, sort~ : String, order~ : String) -> Array[Int]

search_items(items, params) 用 Web 界面的参数(q、owner、packageName、……、rank、momentum、sort、order、expr、ast、limit、offset)执行一次搜索:它用 @query.derive_ast 推导查询树,保留 evaluate 成立的记录(树没有意图时保留全部),用 sort_indices 排序,并返回由 @query.page_limit 和 @query.page_offset 给出的那一页。indices 指向 items;total 是分页之前的匹配数。无效的查询或标签会抛出 QueryError。

sort_indices 对记录下标排序:

  • 空白的 sort 在树有意图时为 relevance,否则为 position;空白的 order 对 relevance、name、position 和 age 为 asc,对其他为 desc;未知的 sort 按 score 的方式排序;
  • relevance 按相关度排序(最高的在前),忽略 order;
  • 其他每个排序键都按所选方向比较其值(score、score_growth_30d、download_count、dependent_count、recent_dependent_count、updated 对应的发布年份、external_dependent_count、dependent_owner_count、rank_position、days_since_release);name 用 collate 比较名称;
  • 无论方向如何,并列都先按名次(最好的在前)、再按 collate 比较名称(升序)打破,最后按下标打破。

文本

pub fn normalize_text(String) -> String
pub fn collate(String, String) -> Int
pub fn runtime_version() -> String

normalize_text 像 ECMAScript toLowerCase 一样把 ASCII、Latin-1、拉丁文扩展 A、希腊文和科普特文、西里尔文(U+0400 到 U+052F)以及全角拉丁大写字母转为小写,只是 Σ 总是变为 σ;其他字符保持不变。collate 比较名称:第一个排在前面时为负,仅当两个字符串完全相同时为零。对 ASCII 而言,它就是英文浏览器中 localeCompare 给出的 CLDR 根排序(标点,然后是数字,然后是忽略大小写的字母,然后小写排在大写之前)。runtime_version 为 "static-search-v2"。

test "text" {
  inspect(@static_search.normalize_text("JSON Ärger"), content="json ärger")
  let names = ["b/x", "A/x", "a-b/x", "a/x"]
  names.sort_by(@static_search.collate)
  debug_inspect(names, content=(
    #|["a-b/x", "a/x", "A/x", "b/x"]
  ))
}

JavaScript 边界

moon build src/static_search --target js 写出带有这些导出项的 ES 模块;scripts/build_moonbit.mjs 把它复制到 lib/moonbit/static_search/,再由 lib/static-search.ts 包装。

导出项类型含义
load_index(json: string) => number解析一个索引文档并保留它;返回条目数,或 -1(保留之前的索引)
search(params_json: string) => string在已加载的索引上搜索,返回 {"ok":{"indices":[…],"total":n}} 或 {"error":"<message>"}
evaluate_json(item_json: string, ast_json: string) => stringevaluate 的信封
relevance_json(item_json: string, ast_json: string) => stringrelevance 的信封
normalize_text(input: string) => string同上
runtime_version() => string"static-search-v2"

基于它的 worker 协议见 static_search 设计。