okf-search-native 0.2.0 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,70 +1,152 @@
1
1
  # `okf-search-native`
2
2
 
3
- The Rust/Tantivy `napi-rs` search backend for prepared Open Knowledge Format
4
- documents. It is a lower-level boundary: callers prepare documents in the
5
- existing OKF layer and pass `PreparedDocument[]` to `NativeOkfSearch`.
3
+ Search [Open Knowledge Format (OKF)](https://github.com/GoogleCloudPlatform/open-knowledge-format)
4
+ collections at native speed from Node.js. `okf-search-native` builds an in-memory
5
+ index with Rust and Tantivy and returns the best matching section from each
6
+ document.
6
7
 
7
- This package does **not** provide `openOkf`, Markdown parsing, preparation,
8
- MiniSearch integration, ranking parity, or browser support.
8
+ Use the package root for Markdown files or strings. Most users should start
9
+ there. Use `okf-search-native/prepared` only when your application already
10
+ produces prepared OKF documents.
9
11
 
10
- ## Supported runtime matrix
12
+ ## Install
11
13
 
12
- The initial release is tested with Node `>=22.19.0` and Node-API 8 on:
14
+ ```sh
15
+ npm install okf-search-native
16
+ ```
13
17
 
14
- | Platform | Native artifact |
15
- | --- | --- |
16
- | macOS x64 | `okf-search-native.darwin-x64.node` |
17
- | macOS arm64 | `okf-search-native.darwin-arm64.node` |
18
- | Windows x64 (MSVC) | `okf-search-native.win32-x64-msvc.node` |
19
- | Linux x64 (glibc >= 2.17) | `okf-search-native.linux-x64-gnu.node` |
18
+ The package requires Node.js `>=22.19.0` and includes TypeScript declarations.
19
+ See [Requirements and tested platforms](#requirements-and-tested-platforms) for
20
+ the available native artifacts.
20
21
 
21
- Linux musl/Alpine, Linux arm64, Windows arm64, Bun, Deno, browsers, and other
22
- Node versions are not claimed by this release. A published tarball contains
23
- all four artifacts. A local host build normally contains only its host
24
- artifact.
22
+ ## Raw Markdown API
23
+
24
+ `createOkfSearch(documents)` synchronously indexes Markdown already in memory.
25
+ `openOkf(root)` recursively reads lowercase `.md` files from a Node.js
26
+ directory. Files named exactly `index.md` or `log.md` are reserved and are not
27
+ indexed.
28
+
29
+ ```js
30
+ import {
31
+ createOkfSearch,
32
+ openOkf,
33
+ validateOkfDocument,
34
+ } from "okf-search-native";
35
+
36
+ const document = {
37
+ path: "notes/memory.md",
38
+ markdown: "---\ntype: note\n---\nMemory safety matters.\n",
39
+ };
40
+
41
+ const validation = validateOkfDocument(document);
42
+ const index = createOkfSearch([document]);
43
+ const hits = index.search("memory", { limit: 10, fields: ["body"] });
44
+
45
+ const directoryIndex = await openOkf("./knowledge");
46
+ directoryIndex.ingest({
47
+ path: "notes/new.md",
48
+ markdown: "---\ntype: note\n---\nNew material.\n",
49
+ });
50
+ directoryIndex.remove("notes/new.md");
51
+ ```
52
+
53
+ Both constructors return an in-memory search handle. `ingest` adds or replaces
54
+ one document after successful validation. `remove` changes only the current
55
+ index, not its source file. Reopening a directory rebuilds the index from the
56
+ files on disk.
57
+
58
+ ### Search behavior
59
+
60
+ Search supports any or all term matching, field selection and boosts, fuzzy
61
+ matching, final-term prefix matching, and filters for OKF type, tags, status,
62
+ trust tier, staleness, and conformance.
63
+
64
+ Results contain at most one hit per document. Each hit represents its
65
+ highest-ranked matching section and includes the document path, heading path,
66
+ line range, matched fields, and snippet. The handle also provides `listTypes()`
67
+ and `listDegradedDocuments()` for inspecting the current collection.
68
+
69
+ ### Validation
70
+
71
+ `validateOkfDocument` checks one Markdown document without changing an index.
72
+ A strict document is valid and indexable. A degraded document remains indexable
73
+ and searchable, with diagnostics describing fields that need repair. A document
74
+ with a fatal path, parsing, Markdown, or `type` problem is not indexable.
75
+ Expected validation failures are returned as diagnostics rather than thrown.
25
76
 
26
- ## API
77
+ See the [OKF v0.2 specification](https://github.com/GoogleCloudPlatform/open-knowledge-format/blob/ad30107c31c06aec8a7d5636e0d1058118604e6f/SPEC.md)
78
+ for the document format and field semantics.
79
+
80
+ ### Differences from `okf-minisearch`
81
+
82
+ The native backend uses Tantivy, so its ranking, scores, snippets, and fuzzy
83
+ candidates can differ from `okf-minisearch`. Browser use is not supported.
84
+ `autoSuggest` is also unsupported and throws an `OkfError` with code
85
+ `ERR_OKF_UNSUPPORTED`.
86
+
87
+ ## Prepared API
88
+
89
+ Most users can skip this section. Use the prepared API when another part of
90
+ your application already produces `PreparedDocument` values and you want to
91
+ pass them directly to the native backend.
27
92
 
28
93
  ```js
29
- const { NativeOkfSearch } = require("okf-search-native");
94
+ import { NativeOkfSearch } from "okf-search-native/prepared";
30
95
 
31
96
  const index = NativeOkfSearch.fromPrepared(preparedDocuments);
32
- const hits = index.search("memory", { limit: 10, fields: ["title", "body"] });
97
+ const hits = index.search("memory", { limit: 10, fields: ["body"] });
33
98
  index.ingestPrepared(preparedDocument);
34
- index.removeDocument({ documentId: "docs/old", path: "docs/old.md" });
35
- index.listTypes();
36
- index.listDegradedDocuments();
99
+ index.removeDocument("docs/old");
37
100
  ```
38
101
 
39
- `autoSuggest` is deliberately unsupported and throws an error containing
40
- `[ERR_OKF_UNSUPPORTED]`. It does not return search hits as suggestions.
102
+ `fromPrepared` builds an index from prepared documents. `ingestPrepared`
103
+ replaces every indexed section owned by one document, and `removeDocument`
104
+ removes them together. `PreparedDocument` contains document-wide metadata once;
105
+ each `PreparedSection` contains only its ID, heading path, text, and line
106
+ bounds. The DTO declarations are exported from `okf-search-native/prepared`,
107
+ not from the package root.
108
+
109
+ ## Requirements and tested platforms
110
+
111
+ Linux x64 and macOS x64/arm64 are fully supported. Windows x64 is experimental.
112
+ All targets require Node.js `>=22.19.0` and use Node-API 8.
113
+
114
+ | Platform | Native artifact |
115
+ | --- | --- |
116
+ | macOS x64 | `okf-search-native.darwin-x64.node` |
117
+ | macOS arm64 | `okf-search-native.darwin-arm64.node` |
118
+ | Windows x64 (MSVC) | `okf-search-native.win32-x64-msvc.node` |
119
+ | Linux x64 (glibc >= 2.17) | `okf-search-native.linux-x64-gnu.node` |
120
+
121
+ Linux musl/Alpine, Linux arm64, Windows arm64, Bun, Deno, browsers, and other
122
+ Node versions are not covered by this matrix.
41
123
 
42
124
  ## Development
43
125
 
44
- Use Rust `1.88.0` (the repository toolchain file installs `rustfmt` and
45
- `clippy`):
126
+ Development requires Rust `1.88.0`:
46
127
 
47
128
  ```sh
48
129
  pnpm install
49
130
  pnpm --filter okf-search-native run build
50
131
  pnpm --filter okf-search-native run check:rust
51
- pnpm --filter okf-search-native run test:rust
52
- pnpm --filter okf-search-native run test:types
53
- pnpm --filter okf-search-native run test:runtime
54
132
  pnpm --filter okf-search-native run test
55
133
  ```
56
134
 
57
- `test:types` compiles against the `index.d.ts` generated by `napi build`.
58
- `index.js` and `index.d.ts` are kept with the package so it remains inspectable
59
- independently of the spike.
135
+ ### Build output
60
136
 
61
- Before a multi-target release, copy the four tested `.node` files into this
62
- package root and run:
137
+ `napi build` generates `native.cjs`, `native.d.cts`, and the host `.node`
138
+ artifact. The package facade build writes `dist/index.cjs`, `dist/index.mjs`,
139
+ `dist/index.d.cts`, `dist/index.d.mts`, and `dist/index.d.ts`. Generated native
140
+ loader names are internal and are not package-root exports.
63
141
 
64
- ```sh
65
- pnpm run verify:release-artifacts
66
- pnpm pack --pack-destination /tmp --json
67
- ```
142
+ ### Release artifacts
143
+
144
+ For multi-target candidate assembly, copy the four tested `.node` files into
145
+ the package root, then run `pnpm run verify:release-artifacts`. The verifier
146
+ derives the required artifact names from the checked-in target list and
147
+ rejects missing or extra native files. CI also uses its `glibc <artifact>` mode
148
+ to reject Linux addons that import symbols newer than `GLIBC_2.17`.
149
+
150
+ ## License
68
151
 
69
- The verifier derives the required artifact names from `package.json`'s
70
- checked-in target list and rejects missing or extra native files.
152
+ [MIT](../../LICENSE)