Skip to content

fix(search): exact-ID lookup, text fallback, relevance floor + WAF bypass - #500

Merged
neuromechanist merged 3 commits into
devfrom
feature/dataset-search-fix
May 14, 2026
Merged

neuromechanist merged 3 commits into
devfrom
feature/dataset-search-fix

Conversation

@neuromechanist

@neuromechanist neuromechanist commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

nemar dataset search was returning zero results for everything, including exact IDs like nm000103 and natural-language queries like sleep eeg. Three independent things were broken — all fixed here.

Root causes

  1. Stale CI secret: the hourly catalog-sync workflow has been failing with HTTP 403 since 2026-04-17 (day after SCCN cutover). The PROD_ADMIN_API_KEY GitHub secret held a token whose user lacks admin on SCCN. Fixed out-of-band by rotating to nemarAdmin's key.
  2. Cloudflare WAF on api.nemar.org: even with the right key, the ~2.5 MB catalog POST from GitHub-runner IPs is 403d by the zone WAF as a suspected bot. The body of the failed response is a Cloudflare challenge token — the request never reaches the Worker.
  3. Search endpoint had no fallback: when Vectorize returned 0 hits (because empty, stale, or because the query was a literal ID), the handler returned [] instead of falling back to the D1 nemar_catalog LIKE search that already exists.

Changes in this PR

backend/src/routes/datasets.ts — /datasets/search handler:

  • Exact dataset-ID short-circuit: queries matching ^(nm|ds)\d{6}$ go straight to D1 with method: \"exact_id\". Embeddings cannot meaningfully match opaque IDs.
  • Empty-Vectorize fallback: when semanticSearch returns zero hits, fall back to textSearch (method: \"text_fallback\").
  • Hit merging: when semantic finds hits, also merge in text-search matches it missed, preserving ranked semantic order at the head.
  • Tightened the existing catch-all so it routes through the same fallback.
  • SearchResult is now exported from the search service.

.github/workflows/catalog-sync.yml:

  • PROD_API_URL switched from https://api.nemar.org to https://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 by ADMIN_API_KEY on the Worker side, so no security regression.

Verification done

  • Manually fetched the 591-record catalog and POSTed to https://api.nemar.org/admin/catalog/sync with the rotated admin key: HTTP 200, 587 records synced, 587 indexed. Vectorize index nemar-dataset-index now reports vectorCount: ~590.
  • Manually triggered catalog-sync.yml on this branch (run 25880375883): green, both prod and dev steps succeeded.
  • Live search now returns sensible results:
    • ?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's method: exact_id path fixes that on deploy.

Test plan

  • bunx tsc --noEmit passes in backend/
  • Manual catalog-sync run is green end-to-end on the branch
  • Vectorize index repopulated; semantic search returns relevant hits
  • After deploy: curl 'https://api.nemar.org/datasets/search?q=nm000103' returns the matching row with method:\"exact_id\"
  • After deploy: empty-result queries still return 200 with count: 0

Closes #499

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.
@github-actions

github-actions Bot commented May 14, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR500

Version: 0.8.16-PR500.5c376c0
Commit: 5c376c0


🤖 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
@neuromechanist neuromechanist added the bug Something isn't working label May 14, 2026
@neuromechanist neuromechanist changed the title fix(search): D1 text fallback + exact dataset-ID lookup fix(search): exact-ID lookup, text fallback, relevance floor + WAF bypass May 14, 2026
@neuromechanist
neuromechanist merged commit 97e9d0f into dev May 14, 2026
9 checks passed
@neuromechanist
neuromechanist deleted the feature/dataset-search-fix branch May 14, 2026 19:29
@neuromechanist neuromechanist mentioned this pull request May 14, 2026
3 of 5 tasks

This branch was previously deployed

1 inactive deployment
npm — 5c376c0b Deployed May 14, 2026 by neuromechanist via publish #900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant