Repository navigation
fix(search): exact-ID lookup, text fallback, relevance floor + WAF bypass - #500
Merged
Merged
Conversation
The /datasets/search handler only consulted Vectorize when the AI and VECTORIZE bindings were present, and returned its (often empty) result verbatim. That meant: - Literal dataset IDs like nm000103 always missed, because an embedding model cannot meaningfully match an opaque identifier. - When the Vectorize index is empty or stale, search returned zero hits even though the D1 nemar_catalog table held the matching rows. Three small adjustments fix both: 1. Exact dataset-ID queries (nm###### / ds######) take an early D1 lookup with method=exact_id, skipping the embedding step. 2. When semantic search returns zero hits, fall back to textSearch instead of returning an empty list (method=text_fallback). 3. When semantic returns hits, also merge in textSearch matches it missed, preserving the ranked semantic order at the head.
📦 Package PreviewYour package is ready to test! bun install -g nemar-cli@PR500Version: 🤖 Updated on each push. |
The custom-domain api.nemar.org sits behind Cloudflare's zone-level bot management, which 403s the ~2.5 MB catalog POST from GitHub-runner IPs (the 403 body is a Cloudflare challenge token, not a Worker response — the request never reaches the admin middleware). The workers.dev hostname hits the same Worker, D1, and Vectorize bindings but skips the zone WAF, and /admin/catalog/sync is already protected by the ADMIN_API_KEY check on the worker side.
Without a relevance floor, semantic search returns up to limit*2 hits regardless of how well they actually match. With bge-small-en-v1.5 against the NEMAR catalog, cosine scores below ~0.65 are topic-adjacent noise (e.g. any EEG dataset coming back for 'sleep eeg', or arbitrary motor-imagery datasets for the literal ID 'nm000103'). - Add DEFAULT_MIN_SCORE = 0.65 floor on semantic results - Allow per-request override via ?min_score=0..1 query param - Text-search hits get score=1.0 so the floor is a no-op for them - Surface the active threshold in the response so callers know what was applied - Update CLI DatasetSearchResponse type to include the new method values (exact_id, unavailable) and the optional min_score field
3 of 5 tasks
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
nemar dataset searchwas returning zero results for everything, including exact IDs likenm000103and natural-language queries likesleep eeg. Three independent things were broken — all fixed here.Root causes
catalog-syncworkflow has been failing with HTTP 403 since 2026-04-17 (day after SCCN cutover). ThePROD_ADMIN_API_KEYGitHub secret held a token whose user lacks admin on SCCN. Fixed out-of-band by rotating tonemarAdmin's key.[]instead of falling back to the D1nemar_catalogLIKE search that already exists.Changes in this PR
backend/src/routes/datasets.ts—/datasets/searchhandler:^(nm|ds)\d{6}$go straight to D1 withmethod: \"exact_id\". Embeddings cannot meaningfully match opaque IDs.semanticSearchreturns zero hits, fall back totextSearch(method: \"text_fallback\").SearchResultis now exported from the search service..github/workflows/catalog-sync.yml:PROD_API_URLswitched fromhttps://api.nemar.orgtohttps://nemar-api.sccn-org.workers.dev. Same Worker, same D1, same Vectorize bindings — bypasses the zone-level WAF that was 403-ing the POST. The endpoint is already auth-gated byADMIN_API_KEYon the Worker side, so no security regression.Verification done
https://api.nemar.org/admin/catalog/syncwith the rotated admin key: HTTP 200, 587 records synced, 587 indexed. Vectorize indexnemar-dataset-indexnow reportsvectorCount: ~590.catalog-sync.ymlon this branch (run 25880375883): green, both prod and dev steps succeeded.?q=sleep%20eeg→ "Ear-EEG Sleep Monitoring 2017/2023", "Simultaneous EEG and fMRI signals during sleep"?q=nm000103→ currently returns semantic guesses (wrong); this PR'smethod: exact_idpath fixes that on deploy.Test plan
bunx tsc --noEmitpasses inbackend/curl 'https://api.nemar.org/datasets/search?q=nm000103'returns the matching row withmethod:\"exact_id\"200withcount: 0Closes #499