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
1 つのパッケージをクエリに照らして検査する@static_search.evaluate, @static_search.relevance

クイックスタート

モジュールを追加し、2 つのパッケージをインポートします。

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 に読み込み、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最近の依存元が 5 つ以上ある急成長中のパッケージ
age<=30 AND owners>=2過去 30 日以内にリリースされ、少なくとも 2 人のほかのオーナーに使われているもの

インターフェースのフラットな絞り込みも使えます。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 は両方の単語に一致し、残りの 2 つはそれぞれ 1 つずつ一致して順位順に並びます(ここでは順位がすべて 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)では、同じテキストが 1 つの部分文字列 json parser になります。
  • フィールドの演算子。 score:180 は 180 以上ではなく score = 180 を意味します。score>=180 を使ってください。
  • NOT は関連度を上げない。 肯定的な項だけが数えられるので、NOT rank=D OR json では json に言及するパッケージがランクに関係なくすべて残りより前に来て、その中ではスコア順になります。
  • インデックスのフィールド。 スコアリング v2 より前にエクスポートしたインデックスには rank_position やその他の新しい件数がなく、load_index はそれを拒否します。データをエクスポートし直してください。

次のステップ