Skip to content

fix(discovery): rank every match and page by ranking position (#676) - #808

Open
Neziahtech wants to merge 2 commits into
Divinity-Labs:mainfrom
Neziahtech:fix/discovery-search-ranking-676
Open

Neziahtech wants to merge 2 commits into
Divinity-Labs:mainfrom
Neziahtech:fix/discovery-search-ranking-676

Conversation

@Neziahtech

@Neziahtech Neziahtech commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #676

The problem

GET /discovery/search did not search — it sampled. It pulled the newest limit × 5 rows, scored those in memory, and returned the best of what it happened to have fetched. Three consequences:

  • an older strong match was unreachable: no page, no ordering, no cursor position could bring it back, because it had never been fetched;
  • totalMatched was the length of one page, not a count of matches, so a caller could not tell a thin result set from a small corpus;
  • paging keyed on a row id, so continuing a search could repeat or skip rows whenever the ranking disagreed with the fetch order.

What this changes

Filtering moves into the database, per term. buildSearchWhere splits the query into terms (tag terms included) and each one is matched with mode: 'insensitive' contains against serviceName, description and url, so the candidate set is what matches rather than what is recent.

The candidate set is an explicit, stated bound. DISCOVERY_SEARCH_CANDIDATE_CAP = 500 rows are ranked; take is cap + 1 so the job of saying "there was more" costs one row, not a hidden truncation. When the search matches more than the cap the response carries truncated: true — the ranking covers the newest 500 matches and the API admits it rather than pretending the older matches do not exist.

totalMatched is a count over the same where, so it is the number of matches, not the size of one page, and it stays truthful when truncated is set.

The cursor carries the ranking position. It encodes (score, id) of the last returned item and the next page resumes after that position in the ranked list, so pages neither repeat nor skip. A cursor that is well-formed but points at nothing in this result is a 400 naming the reason; a degenerate/empty cursor is the first page, like every other paged route here.

Validation happens before the database. limit goes through parseLimit with DISCOVERY_SEARCH_DEFAULT_LIMIT = 20 / DISCOVERY_SEARCH_MAX_LIMIT = 100, so a non-numeric limit is a 400 and never reaches Prisma; an unrecognised network is a 400 rather than a search of the wrong one. A real database failure is still a 503 — the two are not conflated.

req.network selects the network, so x-network behaves here exactly as it does on every other route (the route previously read only the query parameter).

Generated artefacts

openapi.yaml documents the new truncated field, the cap, the cursor semantics and the 400s; openapi.json and clients/typescript/src/schema.d.ts were regenerated with npm run openapi:gen and npm run client:gen, so CI's byte-checks of the spec and the client are satisfied from the source of truth rather than by hand-editing a generated file. Because the route now documents the shared NetworkQuery parameter — which is what ?network=/x-network actually selects on here — npm run snippets:gen also changed the two discovery-search snippets (snippets/typescript/service.ts, snippets/python/service.py) to pass network, that parameter carrying default: testnet. tests/snippets.test.ts byte-compares those files against the generator, so they are committed regenerated rather than hand-edited.

How it is proved

All in src/__tests__/discoverySearch.test.ts, against mocked Prisma the existing tests already mock:

  • ranks an old strong match above a new weak one — the case the old fetch order made impossible.
  • reaches every ranked row by following nextCursor, with no repeats and no gaps — walks the whole result set page by page and asserts the union is exactly the ranking.
  • returns no nextCursor on the last page, even when the page is full.
  • carries the ranking position in the cursor, not a row id; rejects a well-formed cursor that is not a position in this result with 400; treats a degenerate cursor as the first page, like every other paged route.
  • reports the database match count, not the length of one page and says so when the query matches more rows than one search ranks.
  • filters each query term separately, including against tags (#676); pushes ILIKE term filtering into Prisma database where clause with deterministic ordering — the ordering the keyset depends on is asserted, not assumed.
  • answers for the x-network header instead of only the query parameter (#676); rejects an unrecognised network with 400 rather than searching the wrong one; rejects a non-numeric limit with 400 instead of a 503 (#676); clamps the limit into its documented range; returns HTTP 503 when the database query fails.

The scoring functions themselves were left alone, so the pre-existing quality/relevance score tests still pass unchanged — the change is which rows reach the scorer, not how rows are scored. No test was weakened to make this pass, and nothing touching the live network is required: every case stubs Prisma.

Out of scope

parseLimit, encodeCursor/decodeCursor and the apiError/error-code shape are reused as they are — no shared helper was modified, so no other route's paging or validation changes.

GET /discovery/search ranked only the newest limit*5 rows, so older strong
matches were unreachable and totalMatched was not the total. Filtering now
happens in the database per query term (including tag terms split from the
query), the candidate set is an explicit 500-row bound that reports `truncated`
instead of hiding the rest, totalMatched comes from a count over the same where,
and the cursor carries the (score, id) ranking position so pages neither repeat
nor skip. Malformed or unknown cursors and a non-numeric limit are 400s that
never touch the database; a real database error is still 503. Network now comes
from req.network, so x-network selects it like every other route.

Documented in openapi.yaml; openapi.json and the TypeScript client regenerated.
@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@Neziahtech Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Discovery search ranks only the newest rows, pages in circles and blames the database for a bad limit

1 participant