fix: prioritize exact matches in Algolia search - #3103
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
2 Skipped Deployments
|
📝 WalkthroughSummary by CodeRabbit
WalkthroughAlgolia package searches now retrieve full hit data, detect exact package-name matches, place exact matches first, remove duplicates, and validate this behaviour with ChangesAlgolia exact-match search
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
test/nuxt/composables/use-algolia-search.spec.ts (1)
36-54: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCover duplicate removal and the non-match path.
The main mock contains only
otherHit, so the duplicate-removal filter is never executed. A regression that removes this filter would still pass. IncludeexactHitin the main hits and assert that it appears once. Add a case where the returned hit has a differentname; assertpackageExistsisfalseand the hit is not promoted.Proposed test adjustment
results: [ - { hits: [otherHit], nbHits: 2 }, + { hits: [otherHit, exactHit], nbHits: 2 }, { hits: [exactHit], nbHits: 1 }, ],🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/nuxt/composables/use-algolia-search.spec.ts` around lines 36 - 54, The useAlgoliaSearch test must exercise duplicate removal and the non-match behavior. Update the existing mock data and assertions around searchWithSuggestions to include exactHit in the main hits, verify it appears only once, and add a separate case with a hit whose name differs from checkPackage that asserts packageExists is false and the hit is not promoted.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@app/composables/npm/useAlgoliaSearch.ts`:
- Around line 373-380: Use the verified exact-name predicate from packageExists
when deciding whether to promote exactHit in the searchResult.objects ordering.
Update the exactHit promotion condition so only a hit whose name matches
checks?.checkPackage is passed to hitToSearchResult; leave unverified hits in
their existing order.
- Line 372: Update the exactHit assignment in the Algolia search flow to safely
handle responses without a hits array by falling back to an empty array before
accessing the first element. Preserve the existing behavior of selecting the
first hit when hits is present.
---
Nitpick comments:
In `@test/nuxt/composables/use-algolia-search.spec.ts`:
- Around line 36-54: The useAlgoliaSearch test must exercise duplicate removal
and the non-match behavior. Update the existing mock data and assertions around
searchWithSuggestions to include exactHit in the main hits, verify it appears
only once, and add a separate case with a hit whose name differs from
checkPackage that asserts packageExists is false and the hit is not promoted.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 8cdc02ef-7174-4856-b9fd-616d575b6f85
📒 Files selected for processing (2)
app/composables/npm/useAlgoliaSearch.tstest/nuxt/composables/use-algolia-search.spec.ts
ghostdevv
left a comment
There was a problem hiding this comment.
just occurred to me, in your before video when you change the dropdown from Relevance then back again it seems to work - is this an issue with the logic somewhere else?
Good catch. The issue is still present on the initial search. Relevance only fetches a small set of results, so the exact package may not be included. Switching to Downloads/wk fetches a larger set for client-side sorting, which includes the exact package. When switching back, the existing logic can then move it to the top. My change uses the exact-package lookup that is already part of the Algolia request, so the exact match can appear first on the initial relevance search without fetching the larger set. |
|
Thanks for your first contribution, @anilloutombam! ✨ We'd love to welcome you to the npmx community. Come and say hi on Discord! And once you've joined, visit npmx.wamellow.com to claim the contributor role. |
🔗 Linked issue
Fixes #2978
🧭 Context
The search page showed “Relevance” as the selected sort option, but an exact package-name match did not always appear first in the initial Algolia results.
📚 Description
I fixed the search UI so that an exact package-name match is placed at the top of the Algolia results when sorting by relevance. The result is also filtered from its previous position to avoid duplicates.
I added a regression test to cover this behavior.
Before
Screen.Recording.2026-07-31.at.3.41.32.PM.mov
After
Screen.Recording.2026-07-31.at.3.42.22.PM.mov