dirsql 0.4.28 → 0.4.29

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.
@@ -3,10 +3,10 @@
3
3
  Ask a question in plain language and get the closest documents back — even
4
4
  when they share no keywords with it. Install
5
5
  [`dirsql-plugin-embeddings`](../plugins.md#dirsql-plugin-embeddings) and
6
- semantic search becomes plain SQL: the plugin's `embed()` function turns
7
- text into vectors, [`sqlite-vec`](https://github.com/asg017/sqlite-vec)'s
8
- `vec_distance_cosine()` measures distance, and `ORDER BY … LIMIT` does the
9
- ranking. No config, no API keys, no services — the model runs locally.
6
+ semantic search becomes plain SQL: the plugin's `embed()` function turns text
7
+ into vectors and loads [`sqlite-vec`](https://github.com/asg017/sqlite-vec),
8
+ which does the distance math. No API keys, no services — the model runs
9
+ locally.
10
10
 
11
11
  Suppose short notes live in `notes/*.md`:
12
12
 
@@ -40,7 +40,32 @@ The first-ever run downloads the model (on the order of a hundred megabytes,
40
40
  with progress on stderr); after that it loads from the local cache. Results
41
41
  print one `path<TAB>distance` line per match, closest first.
42
42
 
43
- ## The SQL behind it
43
+ ## Which shape
44
+
45
+ The one-liner embeds every matched file on every run. That is the right trade
46
+ for a question you ask once, and the wrong one for a corpus you search
47
+ repeatedly — where a stored [`vec0` index](./search-indexes.md#vector-search-vec0)
48
+ embeds each file once, at ingest, and a query embeds only the question:
49
+
50
+ | | This page: path-table | [Vector index](./search-indexes.md#vector-search-vec0) |
51
+ |---|---|---|
52
+ | Setup | none | a `[[table]]` with a `ddl` batch |
53
+ | Freshness | always current — the walk is the read | watcher-maintained; survives restarts under [`--persist`](./persist.md) |
54
+ | Per query | one `embed()` round trip **per matched file**, plus a walk that reads every file's content | one `embed()` round trip **total**, plus an in-process KNN scan |
55
+ | Top-k | `ORDER BY … LIMIT k` | `MATCH … AND k = …` |
56
+ | Good for | a one-off question, a small corpus, an ad-hoc glob | a corpus you query repeatedly |
57
+
58
+ The index only pays off when the table outlives the query — under `--persist`,
59
+ or inside a long-running [`dirsql server`](../reference/cli.md). A one-shot
60
+ `dirsql query` against an ephemeral index rebuilds the table, and therefore
61
+ re-embeds the corpus, before it answers; that is strictly more work than the
62
+ subquery below. The full recipe — the width probe, the `ddl` batch, both
63
+ triggers, and what a model-id edit costs — is
64
+ [Add a search index to a table](./search-indexes.md#vector-search-vec0).
65
+
66
+ The rest of this page is the zero-setup shape.
67
+
68
+ ## The SQL behind the one-liner
44
69
 
45
70
  The one-liner generates and runs ordinary `dirsql` SQL, and you can write it
46
71
  yourself when you want more than ranked paths — a different projection, a
@@ -79,7 +104,9 @@ Reading the query inside-out:
79
104
  and its distance are NULL too — and SQLite sorts NULLs *first* ascending,
80
105
  so without this line the unrankable files take the top-k slots.
81
106
  4. `vec_distance_cosine(...)` computes cosine distance between the two
82
- vectors; `ORDER BY distance LIMIT 3` keeps the three nearest.
107
+ vectors; `ORDER BY distance LIMIT 3` keeps the three nearest. There is no
108
+ `vec0` table here, so `sqlite-vec`'s `MATCH … AND k = …` does not apply —
109
+ for a plain expression, `ORDER BY … LIMIT k` *is* its documented top-k.
83
110
 
84
111
  Structured files compose with SQL's JSON operators — embed one field instead
85
112
  of the whole file:
@@ -93,24 +120,19 @@ ORDER BY vec_distance_cosine(emb, embed('local private models'))
93
120
  LIMIT 10
94
121
  ```
95
122
 
96
- ::: tip Top-k is `LIMIT k`
97
- If you know `sqlite-vec` you may reach for its `MATCH … AND k = 10` idiom.
98
- That syntax belongs to `sqlite-vec`'s `vec0` virtual table, which the table
99
- above does not use: a `[[table]]`'s own `name` is always a per-file row table.
100
- For plain expressions, `sqlite-vec`'s own documented pattern is exactly what
101
- this guide uses: `ORDER BY vec_distance_cosine(...) LIMIT k`. To get the `vec0`
102
- idiom instead, declare the `vec0` table alongside the row table in the same
103
- [`ddl` batch](../reference/config.md#batch-ddl) and fill it from a trigger.
104
- :::
123
+ The same projection works as an `on-file` hook feeding the indexed shape:
124
+ parse the field in the hook, store it as a column, and the trigger embeds it
125
+ once instead of on every query.
105
126
 
106
127
  ## Repeat runs are cheap
107
128
 
108
129
  Computed vectors are cached on disk, keyed on content and model
109
130
  ([vector cache](../plugins.md#vector-cache)) — re-running a search over
110
- unchanged files skips the model entirely and re-embeds only what changed.
111
- And the plugin costs nothing when idle: a query that never calls `embed()`
112
- spawns no worker and loads no model
113
- ([zero cost when unused](../plugins.md#zero-cost-when-unused)).
131
+ unchanged files skips the model entirely and re-embeds only what changed. That
132
+ takes the *inference* out of the shape above, but not the walk or the per-file
133
+ round trip; only a stored index removes those. And the plugin costs nothing
134
+ when idle: a query that never calls `embed()` spawns no worker and loads no
135
+ model ([zero cost when unused](../plugins.md#zero-cost-when-unused)).
114
136
 
115
137
  ## How `embed()` gets into SQL
116
138
 
@@ -155,8 +155,10 @@ ddl = '''
155
155
  CREATE TABLE notes (slug TEXT, title TEXT, body TEXT);
156
156
 
157
157
  -- Width must equal what the probe printed for the model id named below.
158
- CREATE VIRTUAL TABLE notes_vec USING vec0(embedding float[512]);
159
- CREATE TRIGGER notes_vi AFTER INSERT ON notes BEGIN
158
+ CREATE VIRTUAL TABLE notes_vec
159
+ USING vec0(embedding float[512] distance_metric=cosine);
160
+ CREATE TRIGGER notes_vi AFTER INSERT ON notes
161
+ WHEN new.body IS NOT NULL BEGIN
160
162
  INSERT INTO notes_vec(rowid, embedding)
161
163
  VALUES (new.rowid, embed(new.body, 'minishlab/potion-retrieval-32M'));
162
164
  END;
@@ -169,6 +171,29 @@ END;
169
171
  The delete side is an ordinary `DELETE`, not FTS5's `'delete'` command row —
170
172
  `vec0` is a normal-looking table that way.
171
173
 
174
+ Two clauses in there are load-bearing:
175
+
176
+ - **`distance_metric=cosine`.** `vec0` defaults to L2, which is not the metric
177
+ the rest of dirsql's semantic search uses — `vec_distance_cosine()` backs
178
+ both the plugin's one-liner and
179
+ [Search documents by meaning](./search-by-meaning.md). The two agree only for
180
+ vectors of equal length, so as soon as documents embed to vectors of
181
+ different magnitude they rank differently: against three notes, L2 returned
182
+ `a, c, b` where cosine returned `a, b, c`. Declaring the metric makes
183
+ `distance` the number `vec_distance_cosine()` would compute.
184
+ - **`WHEN new.body IS NOT NULL`.** `embed(NULL)` is `NULL`, and `vec0` rejects
185
+ a NULL vector outright — so without the guard a single row whose text is
186
+ missing fails the **whole** table load, not just its own insert:
187
+
188
+ ```
189
+ dirsql query: failed to load config: SQLite error: Inserted vector for the
190
+ "embedding" column is invalid: Input must have type BLOB (compact format) or
191
+ TEXT (JSON), found NULL
192
+ ```
193
+
194
+ With it, that row still lands in `notes`; only its vector is skipped. FTS5
195
+ needs no such guard — it indexes a NULL happily.
196
+
172
197
  Query it with `sqlite-vec`'s KNN form, joined back on `rowid`:
173
198
 
174
199
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dirsql",
3
- "version": "0.4.28",
3
+ "version": "0.4.29",
4
4
  "description": "Ephemeral SQL index over a local directory",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/thekevinscott/dirsql",
@@ -213,10 +213,10 @@
213
213
  ]
214
214
  },
215
215
  "optionalDependencies": {
216
- "@dirsql/lib-linux-x64-gnu": "0.4.28",
217
- "@dirsql/lib-linux-arm64-gnu": "0.4.28",
218
- "@dirsql/lib-darwin-x64": "0.4.28",
219
- "@dirsql/lib-darwin-arm64": "0.4.28",
220
- "@dirsql/lib-win32-x64-msvc": "0.4.28"
216
+ "@dirsql/lib-linux-x64-gnu": "0.4.29",
217
+ "@dirsql/lib-linux-arm64-gnu": "0.4.29",
218
+ "@dirsql/lib-darwin-x64": "0.4.29",
219
+ "@dirsql/lib-darwin-arm64": "0.4.29",
220
+ "@dirsql/lib-win32-x64-msvc": "0.4.29"
221
221
  }
222
222
  }