Search,
without a server.
サーバーなしで、検索。
Mokuji is a file format for search indexes, with libraries and a command line tool to build and read it. The file is uploaded once; each query reads only the few pieces it needs, by HTTP range requests. Mokuji は、検索インデックスのファイル形式と、それを作って読むライブラリとCLIです。ファイルは置くだけ。検索のたびに読むのは、そのうち必要な数か所だけです。
01
The index is a file.
Put it where your files go.
インデックスは、ファイル。
ファイルを置ける場所に置く。
Search usually means a service: a cluster to run, scale and patch, or an API that bills by the request. Mokuji publishes the index instead, as one file that never changes once written, on any storage that honours Range — S3, R2, a CDN. A browser, a Worker or Node opens it with a reader of 13 KB and fetches only the parts a query touches.
検索といえば、ふつうはサービスです。動かし、増やし、更新し続けるクラスタか、リクエストごとに課金される API。Mokuji はかわりにインデックスを公開します。書いたら変わらないファイル1つを、Range に応えるストレージ(S3、R2、CDN)に置くだけ。ブラウザ、Worker、Node が 13 KB の reader で開き、検索が触れる部分だけを取りに行きます。
- Goes awayなくなるもの
- A search server, and keeping it up検索サーバーと、その運用
- Charges per request to a search API検索 API のリクエスト課金
- Remains残るもの
- A file, and somewhere static to put itファイルと、それを置く静的な場所
- A reader in your pageページの中の reader
This page works that way: it is static, and the files it searches sit on object storage behind a CDN. このページもそうです。ページ自体は静的で、検索しているファイルは CDN の裏のオブジェクトストレージに置いてあります。
02
One layout,
many kinds of search.
1つの形式で、
いろいろな検索。
The format does not decide what a term is or what a result carries. A geohash cell can be a term, and the polygon it falls in the result — which is reverse geocoding, with no spatial index. Click the map. 形式は、何を語にするか、結果に何を載せるかを決めません。ジオハッシュのセルを語に、それが落ちるポリゴンを結果にすれば、空間インデックスなしの逆ジオコーディングになります。地図をクリックしてください。
-
Geocoding, by address prefix住所の前方一致(ジオコーディング)
A term is a whole address, so a prefix is one contiguous range.語は住所まるごと。前方一致は、ひと続きの範囲になる。
GIS2 reads · 3 KB2 回 · 3 KB
-
Postal codes, both directions郵便番号 ⇄ 住所
Codes sort among the digits, addresses among the kanji: one dictionary answers both ways.番号は数字の並びに、住所は漢字の並びに。1つの辞書で両方向に引ける。
GIS2 reads2 回
-
Addresses typed wrong打ち間違えた住所
Terms are pairs of characters. A typo breaks two of them; the rest still match.語は2文字ずつの組。打ち間違いで崩れるのは2組だけで、残りは一致する。
GIS5–17 reads · 10–70 KB5–17 回 · 10–70 KB
-
Reverse geocoding逆ジオコーディング
Terms are geohash cells, and a cell's prefixes are the cells that hold it. No spatial index.語はジオハッシュのセル。セルの前方部分が、それを含むセル。空間インデックスはいらない。
GIS2 requests · 20 KB2 リクエスト · 20 KB
-
Full-text search, ranked順位付きの全文検索
Postings of each bigram intersect; BM25 ranks what is left, from statistics in the file.バイグラムごとのポスティングを重ね、残りをファイル内の統計で BM25 順に並べる。
Search6 reads · 16 KB6 回 · 16 KB
-
Nearest-neighbour vectorsベクトルの近傍探索
A cluster is a term and its vectors the postings, which is IVF, the structure vector databases run.クラスタが語、その中のベクトルがポスティング。ベクトル DB が使う IVF そのもの。
AI21 reads21 回
-
A static STAC catalog静的な STAC カタログ
Dates are terms too, so a period is a range of them, and so is a place.日付も語なので、期間はその範囲になる。場所も同じ。
GIS
-
Your own dataあなたのデータで
One line per document, its text and its payload. Build it, upload it.1行に1件、テキストとペイロード。作って、置くだけ。
03
What a query costs
is written down.
1回の検索のコストは、
仕様に書いてある。
For a query of k terms returning m documents, a reader makes at most 2k + m range reads, plus one when it opens the file. The bound is in the specification, and the conformance suite checks the count as well as the answers, for every implementation: the Rust crates, the TypeScript, Python and Go readers, and a Python builder written from the specification alone. k 語で m 件を返す検索の読み出しは、多くて 2k + m 回。ファイルを開くときにもう1回。この上限は仕様に書いてあり、conformance スイートは答えだけでなく読み出しの回数も確かめます。Rust の実装、TypeScript・Python・Go の reader、仕様だけから書いた Python のビルダー、どれも同じケースに答えます。
04
Search,
in three steps.
検索は、
3ステップで。
-
1
Get a fileファイルを用意する
Download one, such as the files this page searches (listed here, each with its licence), or build your own: one document per line, its text and its payload. このページが検索しているファイル(一覧。ライセンスも書いてあります)のようにダウンロードするか、自分で作ります。1行に1件、テキストとペイロード。
brew install reearth/tap/mokuji mokuji build places.tsv places.mokuji -
2
Put it up置いて公開する
On any storage that answers
Rangerequests: S3, R2, a CDN. For a page on another origin, allow CORS and exposeContent-Range.Rangeリクエストに応えるストレージならどこでも。S3、R2、CDN。別オリジンのページから読むなら、CORS を許可してContent-Rangeを公開します。aws s3 cp postal.mokuji s3://my-bucket/ npx wrangler r2 object put my-bucket/postal.mokuji \ --file postal.mokuji --remote -
3
Read itJS で読む
In a browser, a Worker or Node, with a reader of 13 KB and no dependencies. ブラウザでも Worker でも Node でも。13 KB の reader で、依存はありません。
npm install mokujiimport { MokujiIndex } from "mokuji"; const index = await MokujiIndex.open("https://example.com/postal.mokuji"); const { results } = await index.lookup("1000005");
That is all.以上。
05
Offline,
just keep the file.
オフラインなら、
ファイルを置いて終わり。
No storage and no network. Keep the file on disk, in your app or on the device, and ask it from the command line or from the library. The search makes no request, and reads only the parts it needs, not the whole file. ストレージも通信もいりません。ファイルを手元に、アプリの中や端末に置いて、コマンドラインかライブラリで引くだけ。検索は通信をせず、ファイル全体ではなく必要な部分だけを読みます。
Held whole, it stays small. The 2,521 sections of the PLATEAU specifications, ranked and with a snippet from each result's text, take 1.49 MB; bleve takes 6.8 MB for the same, 4.1 MB gzipped. One corpus, measured by us: the conditions are in the README. まるごと持っても小さいまま。PLATEAU 仕様書の 2,521 節を、順位付きで、結果ごとに本文のスニペットも出せる形にして 1.49 MB。同じことを bleve でやると 6.8 MB、gzip しても 4.1 MB です。私たちが1つの corpus で測った数字なので、条件は README に書いてあります。
From the command lineコマンドラインで
$ mokuji search postal.mokuji 1000005
14942 1000005|東京都千代田区丸の内(次のビルを除く)
query: 2 read(s) - 1 dictionary and postings, 1 payload
bytes: 1474 of 2206580
From the library, in the same few lines in each languageライブラリで(どの言語でも同じ数行)
import { openAsBlob } from "node:fs";
import { MokujiIndex } from "mokuji";
const index = await MokujiIndex.open(await openAsBlob("postal.mokuji"));
const { results } = await index.lookup("1000005", 10);
// in a page, a file the user picked
const index = await MokujiIndex.open(input.files[0]);
use mokuji::MokujiIndex;
let index = MokujiIndex::open_file("postal.mokuji").await?;
let found = index.lookup("1000005", 10).await?;
from mokuji import MokujiIndex
index = MokujiIndex.open("postal.mokuji")
found = index.lookup("1000005", 10)
import mokuji "github.com/reearth/mokuji/go"
index, err := mokuji.OpenFile(ctx, "postal.mokuji")
found, err := index.Lookup(ctx, "1000005", 10)
Replace a search server
with a file.
検索サーバーを、
ファイル1つに。
-
JavaScript
npm install mokuji -
Rust
cargo add mokuji -
Python
pip install mokuji -
Go
go get github.com/reearth/mokuji/go -
CLI
brew install reearth/tap/mokuji
GitHub Specification仕様 README Questions and measurements質問・計測の報告