Skip to content

fix(datasets): include dataset_id + source_id in search clause - #539

Merged
neuromechanist merged 2 commits into
devfrom
fix/search-by-id
May 18, 2026
Merged

neuromechanist merged 2 commits into
devfrom
fix/search-by-id

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Symptom

Search by exact dataset id returns 0 results for catalog rows whose enrichment didn't write the id into `nemar_catalog.search_text`. The website's Discover search box, which hits `GET /datasets?search=`, silently hides such datasets even when they're public, active, and freshly reindexed.

Live repro on api.nemar.org today:

query total_count returned
`search=nm000166` 0 0
`search=on002718` 0 0
`search=ds002718` 1 `on002718` (correct via the dedup fix in #534)
`search=nm000103` 1 `nm000103` (only because its search_text happens to include the id)
`search=M3CV` 1 `nm000166` (matches the name, not the id)

`nm000166` is public, active, has `latest_version_doi`, and `data.nemar.org/nm000166/` returns a healthy landing payload with `v1.0.0`.

Cause

`backend/src/routes/datasets.ts:638-645` (the `buildFilterClauses` search block):

-- managed:
AND (LOWER(d.name) LIKE ? OR LOWER(d.description) LIKE ?
     OR LOWER(COALESCE(c.search_text, '')) LIKE ?)

-- catalog-only:
AND LOWER(c.search_text) LIKE ?

Neither side LIKE-matches the dataset id directly. The id only surfaces via `c.search_text`, which is populated inconsistently across the corpus.

Fix

Add explicit id columns to both clauses:

-- managed:
AND (LOWER(d.dataset_id) LIKE ?
     OR LOWER(COALESCE(d.source_id, '')) LIKE ?
     OR LOWER(d.name) LIKE ?
     OR LOWER(d.description) LIKE ?
     OR LOWER(COALESCE(c.search_text, '')) LIKE ?)

-- catalog-only:
AND (LOWER(c.id) LIKE ? OR LOWER(c.search_text) LIKE ?)

No new indexes. The dataset table is a few hundred rows; the extra LIKEs cost nothing measurable.

Test

New regression test: pick any managed dataset from the unfiltered list, assert that `search=` returns it. Catches any future search-clause that drops the id columns.

Acceptance

Searching by exact dataset id returned 0 results for many datasets
because the LIKE clause only matched against name, description, and
nemar_catalog.search_text. search_text isn't uniformly populated --
nm000103 happens to include the id, nm000166 doesn't. Same problem
on the catalog half: c.id was never matched directly, so a catalog
ds row would only surface if its search_text happened to include
the id.

Repro on production:
  GET /datasets?search=nm000166 -> total_count=0 (catalog has it as
    a public active row)
  GET /datasets?search=on002718 -> total_count=0 (same)

Fix: explicitly LIKE-match dataset_id (and source_id for openneuro
mirrors) on the managed side, and c.id on the catalog side. No new
indexes needed; the dataset table is small (a few hundred rows).

Adds a regression test that picks any managed dataset from the
unfiltered list and asserts search=<id> returns it.

Refs nemarOrg/website#1 (Discover search by id).
@github-actions

github-actions Bot commented May 18, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR539

Version: 0.8.20-PR539.035d631
Commit: 035d631


🤖 Updated on each push.

Addresses pr-review-toolkit findings on PR #539:

- The api-test test passes against the buggy code too, because it
  picks whichever managed dataset is first in /datasets?limit=50,
  and that row may already have its id embedded in c.search_text.
  Replace as the contract guard with a deterministic unit test on
  the now-exported buildFilterClauses that asserts dataset_id,
  source_id, and c.id appear in the clauses and the pattern is
  bound the right number of times.
- Pre-existing LIKE-injection gap (a literal '%' or '_' in user
  input was treated as a SQL wildcard) is now propagated through
  the new id columns too. Escape '%', '_', and '\' in the search
  input and pair every LIKE predicate with ESCAPE '\\'. Unit tests
  cover both the escape function and the per-clause ESCAPE
  attachment.
- Export buildFilterClauses + escapeLikePattern so the unit tests
  can call them directly.

The api.test.ts integration test stays as a smoke check but is no
longer the contract guard.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

pr-review-toolkit review (sonnet)

Two reviewers converged on the same top finding (test doesn't catch the bug) plus a pre-existing LIKE-injection gap. Both addressed in 035d631.

Source Finding Resolution
code-reviewer + pr-test-analyzer The api-test passes against the buggy code: it picks whichever managed dataset is first in /datasets?limit=50, which today is nm000154 whose c.search_text already contains the id. Wouldn't catch the original nm000166 regression. Exported buildFilterClauses + new unit test file test/datasets-search.unit.test.ts. Asserts dataset_id, source_id, and c.id are in the LIKE clauses with the correct bind pattern count. Deterministic, no DB dependency.
code-reviewer LIKE wildcard injection (% / _) propagated to the new id columns. search=% would match every row. Added escapeLikePattern() (also exported + unit-tested), escapes %, _, and \. Every LIKE predicate now uses ESCAPE '\'. Unit test counts ESCAPE occurrences per branch (5 managed, 2 catalog) so a future LIKE without ESCAPE fails the test loudly.
pr-test-analyzer Catalog-only and source_id paths untested Both covered in unit tests.
pr-test-analyzer Case-insensitivity drop would silently pass Covered: unit test passes uppercase input and asserts lowercased binds.

Tests: 9/9 unit pass, typecheck clean. The integration test stays as a smoke check; the unit tests are the contract guard going forward.

@neuromechanist
neuromechanist enabled auto-merge (squash) May 18, 2026 11:14
@neuromechanist
neuromechanist merged commit 12bbf23 into dev May 18, 2026
9 checks passed
@neuromechanist
neuromechanist deleted the fix/search-by-id branch May 18, 2026 11:17

This branch was previously deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant