@yaks/fts 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +357 -2
- package/_dist/compile.d.ts +10 -0
- package/_dist/compile.d.ts.map +1 -0
- package/_dist/ddl.d.ts +72 -0
- package/_dist/ddl.d.ts.map +1 -0
- package/_dist/fields.d.ts +37 -0
- package/_dist/fields.d.ts.map +1 -0
- package/_dist/lexicon.d.ts +33 -0
- package/_dist/lexicon.d.ts.map +1 -0
- package/_dist/mod.d.ts +68 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/search.d.ts +36 -0
- package/_dist/search.d.ts.map +1 -0
- package/_dist/term.d.ts +16 -0
- package/_dist/term.d.ts.map +1 -0
- package/compile.js +391 -0
- package/compile.js.map +1 -0
- package/compile.ts +453 -0
- package/ddl.js +820 -0
- package/ddl.js.map +1 -0
- package/ddl.ts +960 -0
- package/deno.json +1 -0
- package/fields.js +155 -0
- package/fields.js.map +1 -0
- package/fields.ts +207 -0
- package/lexicon.js +71 -0
- package/lexicon.js.map +1 -0
- package/lexicon.ts +84 -0
- package/mod.js +68 -0
- package/mod.js.map +1 -0
- package/mod.ts +69 -0
- package/package.json +31 -4
- package/search.js +466 -0
- package/search.js.map +1 -0
- package/search.ts +585 -0
- package/snippet.js +79 -0
- package/snippet.js.map +1 -0
- package/snippet.ts +125 -0
- package/term.js +153 -0
- package/term.js.map +1 -0
- package/term.ts +193 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Jeff Peterson
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,358 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/fts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Full-text search over graph component tables using SQLite FTS5. The package
|
|
4
|
+
creates search indexes and maintenance triggers, extends `@yaks/sql` queries
|
|
5
|
+
with text matching, and returns ranked results with snippets. It uses a database
|
|
6
|
+
connection supplied by the application and opens no storage itself.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
deno add jsr:@yaks/fts
|
|
12
|
+
# or: npx jsr add @yaks/fts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## What gets indexed
|
|
16
|
+
|
|
17
|
+
Mark searchable string properties with `search: true` in the vocabulary:
|
|
18
|
+
|
|
19
|
+
```json
|
|
20
|
+
{
|
|
21
|
+
"$defs": {
|
|
22
|
+
"book": {
|
|
23
|
+
"component": true,
|
|
24
|
+
"type": "object",
|
|
25
|
+
"kind": true,
|
|
26
|
+
"properties": {
|
|
27
|
+
"title": { "type": "string", "search": true },
|
|
28
|
+
"blurb": { "type": "string", "search": true },
|
|
29
|
+
"price": { "type": "number" }
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`fields(vocab)`, available from `@yaks/fts/fields`, selects marked, stored
|
|
37
|
+
scalar text properties. That subpath reads vocabulary metadata without loading
|
|
38
|
+
the SQL index and query implementations. It excludes computed properties,
|
|
39
|
+
references and numbers. Unmarked properties remain readable but are not indexed.
|
|
40
|
+
A vocabulary with no searchable properties produces no indexes or results. An
|
|
41
|
+
optional `pick(prop)` replaces the default selection predicate; combine it with
|
|
42
|
+
`searched(prop)` when narrowing the default.
|
|
43
|
+
|
|
44
|
+
There is **one index per component**, containing all its selected properties.
|
|
45
|
+
For example, `book_fts` contains both `title` and `blurb`. Each is an
|
|
46
|
+
external-content FTS5 table: the index stores terms while the original text
|
|
47
|
+
remains in the component table. Insert, update and delete triggers keep it
|
|
48
|
+
current.
|
|
49
|
+
|
|
50
|
+
A component can also name text its entities are found by on another component,
|
|
51
|
+
with a `search` list: `entry` says `"search": ["content.body"]`. That makes
|
|
52
|
+
`entry_fts`, which indexes `content.body` for entities carrying `entry` and for
|
|
53
|
+
no other entity carrying `content` (tool results). It reads its text through the
|
|
54
|
+
`entry_text` view, which joins the two tables, and two more triggers on `entry`
|
|
55
|
+
index text already written when an entity becomes an entry and remove it when it
|
|
56
|
+
stops being one. Whichever row a transaction writes second does the indexing, so
|
|
57
|
+
the order of a bundle's components does not matter.
|
|
58
|
+
|
|
59
|
+
A component marked `found: false` (@yaks/vocab) keeps its wearers out of every
|
|
60
|
+
word search: a bare word in a query, its ranking and `find()` all leave out an
|
|
61
|
+
entity wearing it, while a read that names it still answers it. @yaks/kernel's
|
|
62
|
+
`quarantined` is one. Its wearers' text stays indexed, so the mark coming off
|
|
63
|
+
finds them again at once; `fields(vocab)` carries those components on each field
|
|
64
|
+
as its `not`.
|
|
65
|
+
|
|
66
|
+
## Substring indexes
|
|
67
|
+
|
|
68
|
+
A `~=` test (`.content.body~=wyrmling`) matches a substring, which a word index
|
|
69
|
+
cannot find, so it reads every value it tests. Mark a stored text property
|
|
70
|
+
`contains: true` and it keeps a substring index instead: `grams(vocab)` selects
|
|
71
|
+
those fields, and `indexed(vocab)` is both lists, what a store builds and its
|
|
72
|
+
`search` extension reads. Each component's are one FTS5 trigram index,
|
|
73
|
+
`<comp>_trigram`, kept by the same triggers as a word index and filled in
|
|
74
|
+
windows by the same `Filler`. It keeps only which rows hold each trigram
|
|
75
|
+
(`detail=none`): on the box's transcripts it is half the size of the text, where
|
|
76
|
+
positions made it 2.4 times. It holds each text's first MiB (`CUT`), since a
|
|
77
|
+
row's trigrams are made inside the write that stores it (about 190 ms a MiB),
|
|
78
|
+
and a longer text carries one more trigram (`PAST`).
|
|
79
|
+
|
|
80
|
+
The rest of a longer text goes in its **tail index**, `<comp>_trigram_tail`, in
|
|
81
|
+
pieces of `CUT`, each starting `SPAN - 1` (1,023) characters before the one
|
|
82
|
+
before it ends, so any run of `SPAN` characters lies whole in the first `CUT` or
|
|
83
|
+
in one piece. The first `pieces(db, fields)` pass with a piece to make creates
|
|
84
|
+
the tail index in the same unit. Each pass makes one piece, a window of about
|
|
85
|
+
190 ms, and the last piece gets a done row (`DONE`). Until then the text is
|
|
86
|
+
**owed**, and a test reads it whole. The box runs this as an @yaks/sqlite
|
|
87
|
+
`Chore`. In stores without that process, longer texts stay owed. The tail index
|
|
88
|
+
reads each piece out of its text through `<comp>_trigram_tail_text`, a view over
|
|
89
|
+
the table of the pieces it holds, `<comp>_trigram_piece`, whose ids are
|
|
90
|
+
`entity * PIECES + i`, the done row at `i = 0`. A text that changes or goes
|
|
91
|
+
takes its pieces with it, and is owed again. `heal` empties a tail index that
|
|
92
|
+
drifted from its piece table or whose head index it rebuilt. `adopt` keeps
|
|
93
|
+
populated, compatible tails and drops empty or stale ones, which the next
|
|
94
|
+
`pieces` pass recreates when needed.
|
|
95
|
+
|
|
96
|
+
The `search` extension answers @yaks/sql's `contains` hook from both: the texts
|
|
97
|
+
holding up to `GRAMS` (8) of the trigrams of the needle's first `SPAN`
|
|
98
|
+
characters, spread from its start to its end (`grammed`), in their first `CUT`
|
|
99
|
+
or a piece, and the owed texts, and whether there are at most `FEW_TEXTS`
|
|
100
|
+
(4,000) of them. A read is then driven from those rows, each still tested, so a
|
|
101
|
+
needle nothing holds reads only the owed texts. Beside a shape the archetype
|
|
102
|
+
index would select by (`.entry !result`), those rows are the candidates, sought
|
|
103
|
+
by key and their shape tested, not picked out of every owner of the shape. A
|
|
104
|
+
needle more rows hold reads in its own order and stops at its page, as it does
|
|
105
|
+
without an index, but tests only the texts the index found: a phrase of common
|
|
106
|
+
words has its trigrams in many texts and its substring in few, and testing every
|
|
107
|
+
text for it read them all. More than `FEW_TEXTS`, a word index that covers the
|
|
108
|
+
property finds the texts holding the needle's whole words in order (`phrased`),
|
|
109
|
+
and where those are few they take the trigrams' place: an index scoped to a
|
|
110
|
+
component (`entry` for `content.body`) only for a read that requires it, which
|
|
111
|
+
@yaks/sql says as `within`.
|
|
112
|
+
`.entry & .content.body~="apps shows the new
|
|
113
|
+
button"` found 18,841 texts by
|
|
114
|
+
their trigrams, 1.1 GB of them, and 117 by "shows the new". Beside a shape, the
|
|
115
|
+
walk of the archetype index tests each owner of the shape for being one of them
|
|
116
|
+
before any of its rows is read. A needle shorter than three characters, and a
|
|
117
|
+
test while `fill` says the index is still being filled, test every text. Bare
|
|
118
|
+
words never read a substring index (`worded(fields)`).
|
|
119
|
+
|
|
120
|
+
## The four pieces
|
|
121
|
+
|
|
122
|
+
Here `db` is @yaks/sql's synchronous `Driver`, which runs a statement with
|
|
123
|
+
`query(statement)`, over an in-memory database. The graph's component tables are
|
|
124
|
+
created before these indexes.
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import { loadVocab } from '@yaks/vocab'
|
|
128
|
+
import { graph } from '@yaks/graph'
|
|
129
|
+
import { storage } from '@yaks/sqlite'
|
|
130
|
+
import { open } from '@yaks/sqlite/db'
|
|
131
|
+
import { fields, find, heal, schema, search } from '@yaks/fts'
|
|
132
|
+
import { compile, screen } from '@yaks/sql'
|
|
133
|
+
import { parse } from '@yaks/query'
|
|
134
|
+
|
|
135
|
+
let title = { type: 'string', search: true }
|
|
136
|
+
let book = { component: true, properties: { title, price: { type: 'number' } } }
|
|
137
|
+
let vocab = loadVocab([{ $defs: { book } }])
|
|
138
|
+
let db = open(':memory:')
|
|
139
|
+
let store = storage(db, vocab)
|
|
140
|
+
for (let statement of store.ddl()) db.query(statement)
|
|
141
|
+
graph({ storage: store, vocab }).apply([
|
|
142
|
+
{ entity: { eid: 'book-1' }, book: { title: 'The Hobbit', price: 12 } },
|
|
143
|
+
])
|
|
144
|
+
|
|
145
|
+
let text = fields(vocab)
|
|
146
|
+
for (let statement of schema(text)) db.query(statement)
|
|
147
|
+
heal(db, text) // populate indexes if the component tables already had rows
|
|
148
|
+
|
|
149
|
+
let within = screen(parse('.book.price<20'), vocab) ?? undefined
|
|
150
|
+
let hits = find(db, text, 'hobbit', { screen: within, limit: 10 })
|
|
151
|
+
// Each hit: { entity: 'book-1', rank: -1.9, snippet: 'A \x01hobbit\x02…' }
|
|
152
|
+
|
|
153
|
+
let statement = compile(parse('hobbit .book.price<20'), vocab, {
|
|
154
|
+
extend: [search(text)],
|
|
155
|
+
})
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The pieces can be used independently:
|
|
159
|
+
|
|
160
|
+
- `fields(vocab, pick?)` selects properties.
|
|
161
|
+
- `schema(fields, reads?)` returns SQL statements to create the indexes and
|
|
162
|
+
triggers. It does not populate indexes from existing rows; call `heal()` or
|
|
163
|
+
use `adopt()` for an existing database. A @yaks/sqlite store makes a
|
|
164
|
+
component's table on its first write; given them as its `over`, it makes each
|
|
165
|
+
index with its table. `adopt()`, `heal()` and `find()` pass over an index
|
|
166
|
+
whose table no write has made yet, and a text clause matches nothing there.
|
|
167
|
+
- `search(fields, db?)` compiles text clauses into FTS5 membership conditions.
|
|
168
|
+
Pass it through the adapter's `extend` option to enable text clauses in
|
|
169
|
+
ordinary storage reads. Given the database's driver it also orders by
|
|
170
|
+
relevance: `.order=search` puts the closest match first and `.order=-search`
|
|
171
|
+
the weakest, ranking once per query among the rows the rest of it admits. The
|
|
172
|
+
best `RANKED` (1000) are put in order and any past them follow. A window
|
|
173
|
+
(`.limit`, `.after`) of a query with words that states no order is ranked the
|
|
174
|
+
same way: a page of a search is its best rows, never its newest. A first page
|
|
175
|
+
(no `.after`) whose words are all its own clauses ranks only as many rows as
|
|
176
|
+
it shows and reads only those, unless the ranking comes up short.
|
|
177
|
+
- `find(db, fields, text, options?)` executes a ranked search, reading each
|
|
178
|
+
index's content source off the file. `hits(db, fields, text, options?)` runs
|
|
179
|
+
the same search with the sources it is given, and returns `null` when there
|
|
180
|
+
are no search terms or indexes; `find()` returns `[]` in those cases. Given a
|
|
181
|
+
reader's `scope` and its `vocab`, `find()` ranks only text that reader may
|
|
182
|
+
see, reading a component no table stands for as worn by nobody.
|
|
183
|
+
|
|
184
|
+
## Matching and ranking
|
|
185
|
+
|
|
186
|
+
Words match prefixes: `research` can match `researching`. Quoted phrases remain
|
|
187
|
+
phrases, and a trailing `*` requests a prefix. Other FTS5 operators are quoted
|
|
188
|
+
as text. The default tokenizer does not stem words.
|
|
189
|
+
|
|
190
|
+
The SQL extension ANDs separate text clauses; each clause can match a different
|
|
191
|
+
component index on the same entity. Ranked `find()`/`hits()` searches require
|
|
192
|
+
all terms to match within one component index. Results across indexes are
|
|
193
|
+
merged, with one result per entity using its best index score and corresponding
|
|
194
|
+
snippet.
|
|
195
|
+
|
|
196
|
+
FTS5 `bm25` supplies the rank, where lower numbers are better matches. One tier
|
|
197
|
+
sits above it: a hit whose stored text is exactly the words, ignoring case
|
|
198
|
+
(`hobbit` for a book titled "The Hobbit" does not; `the hobbit` does), comes
|
|
199
|
+
before every other, so searching a thing's name finds the thing first. bm25
|
|
200
|
+
alone would put a title saying the word twice ahead of a title that is the word.
|
|
201
|
+
Text kept outside its row is compared as stored, so a blob-backed body never
|
|
202
|
+
ranks this way. Equal matches go newest first. No recency or popularity
|
|
203
|
+
weighting is added.
|
|
204
|
+
|
|
205
|
+
A ranking runs a statement a step, and each statement admits through one index's
|
|
206
|
+
screen: workerd's SQLite refuses to name a common table expression once a
|
|
207
|
+
statement holds 100 cursors, each source of each naming's body among them, and
|
|
208
|
+
one index's page under a visibility screen holds dozens. Each index's page, its
|
|
209
|
+
tied places and its exact names are read alone; joining them is arithmetic in
|
|
210
|
+
JavaScript.
|
|
211
|
+
|
|
212
|
+
Under the sort plan, an index is not opened when none of the screen's owners
|
|
213
|
+
carries its component; SQLite asks that from the smaller side. A screen made
|
|
214
|
+
from a disjunction's candidates (@yaks/sql `Selection.whole`) can only be read
|
|
215
|
+
whole, so it is asked about no more than `PROVE` (1,024) owners, and one wider
|
|
216
|
+
opens the index, whose every match is then tested by key. A ranked first page is
|
|
217
|
+
one of the extension's `few` clauses (@yaks/sql `Extension.few`): the statement
|
|
218
|
+
reads the page's owners by key, never the disjunction beside them.
|
|
219
|
+
|
|
220
|
+
A ranking reads a page, not every match: each index hands back only its own best
|
|
221
|
+
`limit` admitted rows through FTS5's `order by rank`, which sorts inside the
|
|
222
|
+
index, and the names through `^`, newest first, until it has `limit`. A yaks.app
|
|
223
|
+
store bills the rows a cursor hands back, so a word in a thousand rows costs
|
|
224
|
+
about as much as one in twenty. `limit` defaults to 20 and snippet `context`
|
|
225
|
+
defaults to 10 words. `plan: 'index'` keeps these bounded index pages for stores
|
|
226
|
+
that bill rows read; `plan: 'sort'` orders relevance and newest-first ties in
|
|
227
|
+
one scan an index, avoiding a second prefix expansion at a tied page boundary.
|
|
228
|
+
`find()` and the query extension choose the sort plan for a shared file driver;
|
|
229
|
+
`hits()` and `ranked()` keep the index plan unless requested otherwise. An
|
|
230
|
+
optional `screen` is SQL selecting the ids of the permitted entities, such as
|
|
231
|
+
the statement `screen()` returns above. Ranking reads only the index. Snippets
|
|
232
|
+
are cut afterwards in one scan per matching index, for the rows returned. FTS5
|
|
233
|
+
retokenizes a whole row to cut a snippet, so rows over 64 KiB instead get a
|
|
234
|
+
short excerpt around a literal search phrase in their first MiB, or the
|
|
235
|
+
beginning when that preview has no literal match. This bounds display work
|
|
236
|
+
without changing membership or rank; the excerpt may mark only that phrase.
|
|
237
|
+
Smaller rows keep FTS5's token, phrase and Unicode highlighting. `find()` reads
|
|
238
|
+
each index's declared content source from the schema so a preview can be sliced
|
|
239
|
+
before it reaches the full-text virtual table. A caller of `hits()` can supply
|
|
240
|
+
the same index-to-source map as `content`; omitting it reads through the index.
|
|
241
|
+
|
|
242
|
+
Snippets mark matches with `OPEN` (`\x01`) and `CLOSE` (`\x02`), not HTML.
|
|
243
|
+
Renderers must escape the text and add their own highlighting.
|
|
244
|
+
|
|
245
|
+
## Text stored outside the component row
|
|
246
|
+
|
|
247
|
+
A blob-backed property stores an address in its component table. Supply
|
|
248
|
+
`blobRead(vocab)` from `@yaks/blob` as `schema()`'s second argument to index the
|
|
249
|
+
resolved text:
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { loadVocab } from '@yaks/vocab'
|
|
253
|
+
import { storage } from '@yaks/sqlite'
|
|
254
|
+
import { open } from '@yaks/sqlite/db'
|
|
255
|
+
import { blobKeywords, blobRead, blobSchema } from '@yaks/blob'
|
|
256
|
+
import { adopt, fields } from '@yaks/fts'
|
|
257
|
+
|
|
258
|
+
let body = { type: 'string', store: 'blob', search: true }
|
|
259
|
+
let vocab = loadVocab([{
|
|
260
|
+
$defs: { post: { component: true, properties: { body } } },
|
|
261
|
+
}], [
|
|
262
|
+
blobKeywords,
|
|
263
|
+
])
|
|
264
|
+
let db = open(':memory:')
|
|
265
|
+
for (let s of [...storage(db, vocab).ddl(), ...blobSchema()]) db.query(s)
|
|
266
|
+
let text = fields(vocab)
|
|
267
|
+
|
|
268
|
+
adopt(db, text, blobRead(vocab))
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
The generated `<comp>_text` view resolves content for snippets and rebuilds. The
|
|
272
|
+
triggers resolve both old and new values, so direct SQL writes and graph writes
|
|
273
|
+
index the same text. A shared `@yaks/sql` read override must supply
|
|
274
|
+
`text(stored)`; a read override with only `expr(owner)` is rejected because a
|
|
275
|
+
delete trigger must resolve the old value after the component row changes.
|
|
276
|
+
|
|
277
|
+
## Maintaining existing indexes
|
|
278
|
+
|
|
279
|
+
`heal(db, fields, { deep?, fill?, derived? })` compares component row counts
|
|
280
|
+
with the FTS index's `_docsize` table and rebuilds inconsistent indexes.
|
|
281
|
+
Counting the external-content index itself would only count its source rows. The
|
|
282
|
+
default count check cannot detect every form of corruption. `deep: true` adds
|
|
283
|
+
FTS5's full integrity check, which is more expensive and suits maintenance runs.
|
|
284
|
+
With a `Filler` (below), an inconsistent index is emptied and refilled in
|
|
285
|
+
windows instead of rebuilt in one statement, and an index the filler is still
|
|
286
|
+
filling is left to it; `derived` is the store's read overrides its triggers read
|
|
287
|
+
through. The return value lists rebuilt or refilling indexes; a failed rebuild
|
|
288
|
+
throws.
|
|
289
|
+
|
|
290
|
+
`adopt(db, fields, reads?, options?)` brings an existing database into the
|
|
291
|
+
generated layout. It preserves indexes whose columns and content source match,
|
|
292
|
+
recreates and rebuilds mismatched indexes, updates changed text views, removes
|
|
293
|
+
other triggers referencing these indexes, and calls `heal()` last. It returns
|
|
294
|
+
`{ recut, dropped, healed }`. A second call with the same configuration makes no
|
|
295
|
+
changes. Review custom triggers before adopting existing indexes.
|
|
296
|
+
|
|
297
|
+
`{ fill }` hands each index `adopt` would rebuild to a `Filler`, which fills it
|
|
298
|
+
in windows behind a cursor instead: `owe(fill)` takes on the emptied index and
|
|
299
|
+
returns the cursor expression, and its triggers keep only the rows the cursor
|
|
300
|
+
has passed until the filler puts the plain ones in place. `pending(fts)` keeps
|
|
301
|
+
an index still being filled guarded on the next call. @yaks/sqlite's owed work
|
|
302
|
+
(`filler`) is one; returning undefined rebuilds as before.
|
|
303
|
+
|
|
304
|
+
A schema installer can use `{ heal: false }` to skip the content census of
|
|
305
|
+
retained indexes. Missing indexes and changed views or writers are still
|
|
306
|
+
rebuilt; unchanged indexes keep their terms without reading their content:
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
import { adopt, fields } from '@yaks/fts'
|
|
310
|
+
import { loadVocab } from '@yaks/vocab'
|
|
311
|
+
import { open } from '@yaks/sqlite/db'
|
|
312
|
+
import { storage } from '@yaks/sqlite'
|
|
313
|
+
import { equal } from '@yaks/testing'
|
|
314
|
+
|
|
315
|
+
let vocab = loadVocab({
|
|
316
|
+
$defs: {
|
|
317
|
+
book: {
|
|
318
|
+
component: true,
|
|
319
|
+
properties: {
|
|
320
|
+
title: { type: 'string', search: true },
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
})
|
|
325
|
+
let db = open(':memory:')
|
|
326
|
+
storage(db, vocab).install()
|
|
327
|
+
adopt(db, fields(vocab), {}, { heal: false })
|
|
328
|
+
equal(adopt(db, fields(vocab), {}, { heal: false }).recut, [])
|
|
329
|
+
db.close()
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
## What it assumes
|
|
333
|
+
|
|
334
|
+
The SQLite layout used by `@yaks/sql` and `@yaks/sqlite`: `entity.id` is an
|
|
335
|
+
integer primary key, `entity.eid` is the public ID, component tables have an
|
|
336
|
+
`entity` owner column, and `tombstone` identifies deleted entities. FTS rowids
|
|
337
|
+
are the integer owner IDs. Ranked searches exclude tombstoned entities.
|
|
338
|
+
|
|
339
|
+
`@yaks/sqlite` can create a compatible `doc_fts` index. The generated statements
|
|
340
|
+
use `if not exists`; use `adopt()` when an existing index's configuration
|
|
341
|
+
differs. For vector similarity ranking, see
|
|
342
|
+
[@yaks/embedding](../embedding/README.md).
|
|
343
|
+
|
|
344
|
+
## Exports
|
|
345
|
+
|
|
346
|
+
The root module is the only export path. It exports field selection (`fields`,
|
|
347
|
+
`grams`, `indexed`, `worded`, `searched`, `indexes`, `indexName`, `gramName`,
|
|
348
|
+
`textName`), DDL and maintenance (`schema`, `heal`, `adopt`), query integration
|
|
349
|
+
(`search`), ranked search (`find`, `hits`), text conversion (`term`, `match`,
|
|
350
|
+
`OPEN`, `CLOSE`), and their supporting types, including `Field`, `Hit` and
|
|
351
|
+
`SearchOpts`. The `Driver` it runs on is `@yaks/sql`'s.
|
|
352
|
+
|
|
353
|
+
## Compatibility
|
|
354
|
+
|
|
355
|
+
Requires SQLite with FTS5. The package has no runtime-specific storage binding;
|
|
356
|
+
the supplied driver determines which runtime it can run in. `find`, `heal` and
|
|
357
|
+
`adopt` require synchronous driver methods; generated SQL can be executed
|
|
358
|
+
through an asynchronous adapter.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Driver, type Extension } from "@yaks/sql";
|
|
2
|
+
import { type Field } from "./fields.js";
|
|
3
|
+
import { type Filler } from "./ddl.js";
|
|
4
|
+
import { type And } from "@yaks/query/core";
|
|
5
|
+
export declare let SEARCH: string;
|
|
6
|
+
export declare let RANKED: number;
|
|
7
|
+
export declare let reach: (q: And) => number | null;
|
|
8
|
+
export declare let FEW_TEXTS: number;
|
|
9
|
+
export declare let search: (fields: Field[], db?: Driver, fill?: Pick<Filler, 'pending'>) => Extension;
|
|
10
|
+
//# sourceMappingURL=compile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sources":["../compile.ts"],"names":[],"mappings":"AA+BA,SAOE,KAAK,MAAM,EAEX,KAAK,SAAS,yBAoBa;AAS7B,SAAS,KAAK,KAAK,sBAA6D;AAEhF,SAAe,KAAK,MAAM,mBAAsC;AAChE,SAAS,KAAK,GAAG,gCAAyE;AAG1F,OAAO,YAAI,eAAiB;AAI5B,OAAO,YAAI,eAAa;AAmBxB,OAAO,YAAI,QAAS,GAAG,QAAM,MAAM,GAAG,IAAI,CAezC;AAOD,OAAO,YAAI,kBAAgB;AAwB3B,OAAO,YAAI,SACT,QAAQ,SACR,KAAK,QACL,OAAO,KAAK,QAAQ,eACnB,UAgTF"}
|
package/_dist/ddl.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Vocab } from "@yaks/vocab";
|
|
2
|
+
import { type Field } from "./fields.js";
|
|
3
|
+
import { type CreateTrigger, type Derived, type Driver, type Expr, type Stmt } from "@yaks/sql";
|
|
4
|
+
/** How much of a text a substring index holds: its first mebibyte. One row's
|
|
5
|
+
* trigrams are made inside the write that stores it, about 190 ms a MiB, and
|
|
6
|
+
* the box's transcripts hold texts of 33 MB. The 50 past it hold 278 MB of
|
|
7
|
+
* their 2.2 GB. */ export declare let CUT: any;
|
|
8
|
+
/** The trigram a substring index holds for a text longer than {@link CUT}:
|
|
9
|
+
* three private-use characters, which no needle holds (a `~=` needle is
|
|
10
|
+
* ASCII), so a test reads each such row whole, as well as the ones its
|
|
11
|
+
* needle's trigrams find, until the text's tail index holds every piece of
|
|
12
|
+
* the rest. */ export declare let PAST: string;
|
|
13
|
+
/** The trigram a tail index holds in a text's done row, once it holds every
|
|
14
|
+
* piece of the text. */ export declare let DONE: string;
|
|
15
|
+
/** The rows a tail index keeps for each text: an entity's are
|
|
16
|
+
* `entity * PIECES + i`, its `i`th piece, and its done row at 0. A SQLite
|
|
17
|
+
* text holds at most 10^9 bytes, 954 pieces of {@link CUT}. */ export declare let PIECES: any;
|
|
18
|
+
/** The longest run of a text one of its pieces is sure to hold whole: each
|
|
19
|
+
* starts SPAN - 1 characters before the one before it ends. A test asks for
|
|
20
|
+
* the trigrams of its needle's first SPAN characters (./compile.ts). */ export declare let SPAN: any;
|
|
21
|
+
/**
|
|
22
|
+
* Make the next piece of the first text a substring index holds only the
|
|
23
|
+
* first {@link CUT} of, and its done row with its last: one window of the
|
|
24
|
+
* work a long text's write leaves, about 190 ms for a piece. Run it in a
|
|
25
|
+
* unit of its own, as @yaks/sqlite's owed work does (`Chore`). False when
|
|
26
|
+
* no standing head index holds text that still owes pieces.
|
|
27
|
+
*/ export declare let pieces: (db: Driver, fields: Field[], derived?: Derived) => boolean;
|
|
28
|
+
export declare let schema: (fields: Field[], derived?: Derived, vocab?: Vocab) => Stmt[];
|
|
29
|
+
/**
|
|
30
|
+
* The text every index reads, one select per index: its properties off the
|
|
31
|
+
* table or view it is kept from, the way its triggers read them. What a
|
|
32
|
+
* whole-store pass over the indexed text reads (./lexicon.ts). Given the
|
|
33
|
+
* file, only the indexes whose tables it holds.
|
|
34
|
+
*/ export declare let sources: (fields: Field[], derived?: Derived, db?: Driver) => Stmt[];
|
|
35
|
+
export type HealOpts = {
|
|
36
|
+
deep?: boolean;
|
|
37
|
+
/** Who refills a drifted index in windows, rather than `rebuild`ing it in
|
|
38
|
+
* one statement. An index it is filling already holds only the rows its
|
|
39
|
+
* cursor has passed, so it is left to it. */ fill?: Filler;
|
|
40
|
+
/** The store's read overrides, which the triggers of an index being
|
|
41
|
+
* refilled read its columns through (`schema`). */ derived?: Derived;
|
|
42
|
+
};
|
|
43
|
+
export declare let heal: (db: Driver, fields: Field[], opts?: HealOpts) => string[];
|
|
44
|
+
export type Adopted = {
|
|
45
|
+
recut: string[];
|
|
46
|
+
dropped: string[];
|
|
47
|
+
healed: string[];
|
|
48
|
+
};
|
|
49
|
+
export type AdoptOpts = Omit<HealOpts, 'derived'> & {
|
|
50
|
+
/** Check retained indexes for drift (default true). Schema installation can
|
|
51
|
+
* keep unchanged indexes without reading their content. */ heal?: boolean;
|
|
52
|
+
};
|
|
53
|
+
/** An index to fill: its name, where it reads its content back from, its
|
|
54
|
+
* columns, and the triggers it keeps once it holds every row. */ export type Fill = {
|
|
55
|
+
fts: string;
|
|
56
|
+
source: string;
|
|
57
|
+
props: string[];
|
|
58
|
+
fields?: string[];
|
|
59
|
+
triggers: CreateTrigger[];
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Someone who fills an index in windows, behind a cursor: its triggers keep
|
|
63
|
+
* only the rows the cursor has passed, each window reads the next rows from
|
|
64
|
+
* the index's content into it, and the last puts {@link Fill.triggers} in
|
|
65
|
+
* place. @yaks/sqlite's owed work is one.
|
|
66
|
+
*/ export type Filler = {
|
|
67
|
+
/** the cursor an index still being filled guards its triggers with */ pending: (fts: string) => Expr | undefined;
|
|
68
|
+
/** take on filling `fill`, which is empty: its cursor, or undefined to have
|
|
69
|
+
* it rebuilt at once */ owe: (fill: Fill) => Expr | undefined;
|
|
70
|
+
};
|
|
71
|
+
export declare let adopt: (db: Driver, fields: Field[], derived?: Derived, opts?: AdoptOpts) => Adopted;
|
|
72
|
+
//# sourceMappingURL=ddl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ddl.d.ts","sources":["../ddl.ts"],"names":[],"mappings":"AAsDA,cAAc,KAAK,2BAAgC;AACnD,SAEE,KAAK,KAAK,sBASQ;AACpB,SAQE,KAAK,aAAa,EAGlB,KAAK,OAAO,EACZ,KAAK,MAAM,EAKX,KAAK,IAAI,EAeT,KAAK,IAAI,yBAMkB;AAwB7B;;;iBAGiB,GACjB,OAAO,YAAI,SAAa;AAExB;;;;aAIa,GACb,OAAO,YAAI,aAAiC;AAE5C;sBACsB,GACtB,OAAO,YAAI,aAAiC;AAE5C;;6DAE6D,GAC7D,OAAO,YAAI,YAAgB;AAE3B;;sEAEsE,GACtE,OAAO,YAAI,UAAc;AAoIzB;;;;;;CAMC,GACD,OAAO,YAAI,SACT,IAAI,QACJ,QAAQ,SACR,UAAS,YACR,OAAO,CAmET;AA6KD,OAAO,YAAI,SACT,QAAQ,SACR,UAAS,SACT,QAAQ,UACP,OAUF;AAED;;;;;CAKC,GACD,OAAO,YAAI,UACT,QAAQ,SACR,UAAS,SACT,KAAK,WACJ,OASC;AAkEJ,YAAY;EAIV,OAAO,OAAO;EACd;;6CAE2C,GAC3C,OAAO;EACP;mDACiD,GACjD,UAAU;;AAQZ,OAAO,YAAI,OACT,IAAI,QACJ,QAAQ,SACR,OAAM,aACL,MAAM,GA+BR;AAKD,YAAY;EACV,OAAO,MAAM;EACb,SAAS,MAAM;EACf,QAAQ,MAAM;;AAmHhB,YAAY,YAAY,KAAK,UAAU;EACrC;2DACyD,GACzD,OAAO,OAAO;;AAGhB;+DAC+D,GAC/D,YAAY;EACV,KAAK,MAAM;EACX,QAAQ,MAAM;EACd,OAAO,MAAM;EACb,SAAS,MAAM;EACf,UAAU;;AAyBZ;;;;;CAKC,GACD,YAAY;EACV,oEAAoE,GACpE,UAAU,KAAK,MAAM,KAAK,OAAO,SAAS;EAC1C;wBACsB,GACtB,MAAM,MAAM,SAAS,OAAO,SAAS;;AAGvC,OAAO,YAAI,QACT,IAAI,QACJ,QAAQ,SACR,UAAS,SACT,OAAM,cACL,QAuFF"}
|