Repository navigation
fix(discovery): rank every match and page by ranking position (#676) - #808
Open
Neziahtech wants to merge 2 commits into
Open
Neziahtech wants to merge 2 commits into
Neziahtech wants to merge 2 commits into
Conversation
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.
|
@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! 🚀 |
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.
Closes #676
The problem
GET /discovery/searchdid not search — it sampled. It pulled the newestlimit × 5rows, scored those in memory, and returned the best of what it happened to have fetched. Three consequences:totalMatchedwas the length of one page, not a count of matches, so a caller could not tell a thin result set from a small corpus;What this changes
Filtering moves into the database, per term.
buildSearchWheresplits the query into terms (tag terms included) and each one is matched withmode: 'insensitive'contains againstserviceName,descriptionandurl, so the candidate set is what matches rather than what is recent.The candidate set is an explicit, stated bound.
DISCOVERY_SEARCH_CANDIDATE_CAP = 500rows are ranked;takeiscap + 1so the job of saying "there was more" costs one row, not a hidden truncation. When the search matches more than the cap the response carriestruncated: true— the ranking covers the newest 500 matches and the API admits it rather than pretending the older matches do not exist.totalMatchedis a count over the samewhere, so it is the number of matches, not the size of one page, and it stays truthful whentruncatedis 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.
limitgoes throughparseLimitwithDISCOVERY_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.networkselects the network, sox-networkbehaves here exactly as it does on every other route (the route previously read only the query parameter).Generated artefacts
openapi.yamldocuments the newtruncatedfield, the cap, the cursor semantics and the 400s;openapi.jsonandclients/typescript/src/schema.d.tswere regenerated withnpm run openapi:genandnpm 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 sharedNetworkQueryparameter — which is what?network=/x-networkactually selects on here —npm run snippets:genalso changed the two discovery-search snippets (snippets/typescript/service.ts,snippets/python/service.py) to passnetwork, that parameter carryingdefault: testnet.tests/snippets.test.tsbyte-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 pageandsays 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/decodeCursorand theapiError/error-code shape are reused as they are — no shared helper was modified, so no other route's paging or validation changes.