zettelkasten.global_index¶
zettelkasten.global_index ¶
Global note-level ANN index for the Zettelkasten.
A single combined kglite store over EVERY box's note vectors, so a large-scope
query (e.g. "all earnings calls this quarter across my universe") is ONE
approximate-nearest-neighbour query instead of an O(#boxes) per-box fan-out.
The per-box .kgl indexes in :mod:zettelkasten.embeddings remain the
authoritative rebuild source and the small-scope / federated path; this store
is a second, disposable derived cache that mirrors them.
Design (mirrors the single memory.kgl precedent):
- One graph at
<ANGELO_DIR>/zettel/_global.kgl. Node id is the COMPOSITEf"{box}::{note_id}"because zettel ids are not unique across boxes. Each node carriesbox,note_id,date(intYYYYMMDDor0),title,type,search_textandtext_hashproperties, so a pre-filtered vector search can restrict recall to a project's boxes and/or a date range BEFORE ranking (kgliteselect().where(...).vector_search(...)). - The embedder, native lock, embed-text builder and quote-skip set are shared
with :mod:
zettelkasten.embeddingsso a note's global vector is byte-identical to its per-box vector (same model, same text). - Disposable: the
.mdfiles are the source of truth. A corrupt or stale_global.kglis rebuilt from the live notes by :meth:GlobalIndex.rebuild;text_hashper node lets a rebuild reuse unchanged vectors instead of re-embedding the whole corpus.
GlobalIndex ¶
A single combined kglite store over all boxes' note vectors.
Source code in zettelkasten/global_index.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
load_cache ¶
Load the persisted store as the LIVE, queryable graph.
Unlike the per-box index (which starts empty and rebuilds every box from
.md on load), the global store loads its prior .kgl directly so it
is immediately queryable across process restarts without walking the whole
corpus. text_hash tracking is recovered so later upserts can skip
unchanged notes; :meth:rebuild is the disposable full-refresh that
re-derives everything from the live .md sources (and evicts any node
for a note deleted while the process was down).
Source code in zettelkasten/global_index.py
save_cache ¶
Persist the kglite graph atomically (temp + rename); disposable.
Source code in zettelkasten/global_index.py
save_cache_debounced ¶
Schedule a debounced :meth:save_cache, coalescing rapid persists.
Source code in zettelkasten/global_index.py
flush_cache ¶
Cancel any pending debounced save and persist immediately.
Source code in zettelkasten/global_index.py
upsert_note ¶
Add/update a single note's global node + vector.
vector may be supplied by the caller (the per-box index already
embedded this exact text) to avoid a second embed on the write path; when
None the note is embedded here. Quote-type notes (EMBED_SKIP_TYPES)
are skipped, matching the per-box index.
Source code in zettelkasten/global_index.py
upsert_notes ¶
upsert_notes(box: str, notes: list['Note'], date: int = 0, *, vectors: dict[str, list[float]] | None = None, skip_unchanged: bool = False) -> None
Add/update many notes for one box in a single batch.
vectors optionally maps note id -> precomputed vector (reused from the
per-box index); any note missing from it is embedded here.
skip_unchanged (used by the warm-mirror path) drops notes whose
text_hash already matches the indexed one, so re-mirroring a box on a
cold access is a near no-op instead of re-adding every node.
Source code in zettelkasten/global_index.py
remove_note ¶
Delete one note's global node (synchronous persist, like delete_note).
Source code in zettelkasten/global_index.py
remove_box ¶
Delete every node belonging to a box (e.g. when the box is removed).
Source code in zettelkasten/global_index.py
reconcile_box ¶
Evict global nodes for a box whose note no longer exists on disk.
Called when a box is (re)built from its .md sources so a note deleted
while the process was down (e.g. an external git pull) cannot linger
as a ghost in the global index — mirroring the per-box index's rebuild
guarantee. Returns the number of evicted nodes.
Source code in zettelkasten/global_index.py
search ¶
search(query: str, top_k: int = 10, *, boxes: Iterable[str] | None = None, date_range: tuple[int, int] | None = None) -> list[tuple[str, str, float]]
Pre-filtered ANN search; returns [(box, note_id, score)].
boxes restricts recall to those boxes (an EMPTY iterable yields no
results — a project with no sources matches nothing). date_range is an
inclusive (lo, hi) on the YYYYMMDD date property (notes with an
unknown date 0 are excluded when a range is given). Both filters are
applied BEFORE ranking so recall for a small project inside a huge corpus
stays correct.
Source code in zettelkasten/global_index.py
rebuild ¶
Rebuild the whole store from the live .md notes across all boxes.
Walks the graphs directory, indexing every real source box (and
_cross), reusing prior vectors whose text_hash still matches so a
rebuild does not re-embed the entire corpus. Returns a small summary.
Source code in zettelkasten/global_index.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | |
covered_boxes ¶
Subset of candidates that currently hold >=1 indexed note.
Lets a caller treat the ANN as the SOLE semantic source for a covered box
— an empty ANN result for it means "no match", not "not indexed" — while
still falling back to a per-box search for uncovered boxes (the memory
tree, federated peers, or a box not yet mirrored in). Derived from the
in-memory composite-id hash map (box::note_id), so it reflects the live
store with no embedder build and no KGLite dialect assumptions. Short-
circuits once every candidate is accounted for (the common fully-covered
scope).
Source code in zettelkasten/global_index.py
build_if_empty ¶
One-time backfill: rebuild ONLY when the store holds no notes yet.
Backward-compat path for repos indexed by an older angelo (no
_global.kgl on disk) — the first warmup after an update finds an empty
store and populates it from the live .md corpus. A store that already
holds notes (loaded from a persisted cache, or warmed incrementally) is
left untouched, so an already-indexed repo is NEVER re-indexed. Returns the
:meth:rebuild summary when a build ran, else None (nothing to do).
Source code in zettelkasten/global_index.py
composite_id ¶
get_global_index ¶
Return the process-wide global index, or None when embeddings are off.
Lazily built and cache-loaded on first use. Returns None when semantic
search is disabled (ZETTEL_SKIP_EMBEDDINGS=1 or no embedder), so callers
transparently fall back to the per-box path.
Source code in zettelkasten/global_index.py
reset_global_index ¶
warm_global_index ¶
Eagerly load the global store so the first query doesn't pay the cold cost.
Loads the persisted .kgl (via :func:get_global_index). With
auto_build set, an EMPTY store (old repo with no persisted index) is then
populated once from the live .md corpus via
:meth:GlobalIndex.build_if_empty — this is the backward-compat backfill so a
repo indexed by an older angelo gets an ANN on the next server start. An
already-populated store is never rebuilt. Returns the index, or None when
embeddings are disabled. Safe to call from a background thread — the load and
the (idempotent) build are lock-guarded.
Source code in zettelkasten/global_index.py
global_index_status ¶
Cheap health snapshot of the global store — never builds the embedder.
Reports the cache file's presence/size/age and, when the singleton is already
built THIS process, its in-memory indexed-note count. Deliberately avoids
constructing the index (which would load the embedding model) so health is
fast even when semantic search has not been touched yet.