Skip to content

fix: prioritize exact matches in Algolia search - #3103

Merged
ghostdevv merged 2 commits into
npmx-dev:mainfrom
anilloutombam:fix/search-default-relevance
Jul 31, 2026
Merged

fix: prioritize exact matches in Algolia search#3103
ghostdevv merged 2 commits into
npmx-dev:mainfrom
anilloutombam:fix/search-default-relevance

Conversation

@anilloutombam

@anilloutombam anilloutombam commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

🔗 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

@vercel

vercel Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
npmx.dev Ready Ready Preview Jul 31, 2026 10:06am
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs.npmx.dev Ignored Ignored Preview Jul 31, 2026 10:06am
npmx-lunaria Ignored Ignored Jul 31, 2026 10:06am

Request Review

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Package searches now recognise exact package-name matches more reliably.
    • Exact matches appear at the top of suggestions, with duplicate results removed.
    • Search results now retain complete package details for improved display and selection.
  • Tests

    • Added coverage confirming exact matches are identified and prioritised ahead of other suggestions.

Walkthrough

Algolia package searches now retrieve full hit data, detect exact package-name matches, place exact matches first, remove duplicates, and validate this behaviour with searchWithSuggestions tests.

Changes

Algolia exact-match search

Layer / File(s) Summary
Exact match detection and result ordering
app/composables/npm/useAlgoliaSearch.ts, test/nuxt/composables/use-algolia-search.spec.ts
The existence check retrieves full Algolia attributes and requires an exact package-name match. An exact match is converted to a search result, placed first, and removed from duplicate results. Tests verify detection and ordering.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address issue #2978 by placing an exact package-name match first and removing duplicate results.
Out of Scope Changes check ✅ Passed The changes are limited to Algolia search behaviour and its regression test, which match the linked issue objectives.
Title check ✅ Passed The title clearly and concisely describes the main change: prioritising exact matches in Algolia search.
Description check ✅ Passed The description explains the search-order issue, the exact-match fix, duplicate removal, and the regression test.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
test/nuxt/composables/use-algolia-search.spec.ts (1)

36-54: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover 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. Include exactHit in the main hits and assert that it appears once. Add a case where the returned hit has a different name; assert packageExists is false and 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

📥 Commits

Reviewing files that changed from the base of the PR and between d727566 and fb8b8fd.

📒 Files selected for processing (2)
  • app/composables/npm/useAlgoliaSearch.ts
  • test/nuxt/composables/use-algolia-search.spec.ts

Comment thread app/composables/npm/useAlgoliaSearch.ts Outdated
Comment thread app/composables/npm/useAlgoliaSearch.ts Outdated

@ghostdevv ghostdevv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@anilloutombam

Copy link
Copy Markdown
Contributor Author

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.

@anilloutombam
anilloutombam requested a review from ghostdevv July 31, 2026 18:33
@ghostdevv
ghostdevv added this pull request to the merge queue Jul 31, 2026
Merged via the queue into npmx-dev:main with commit a468a65 Jul 31, 2026
25 checks passed
@github-actions

Copy link
Copy Markdown

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.

ayo-run pushed a commit to ayo-run/npmx.dev that referenced this pull request Aug 5, 2026
@anilloutombam
anilloutombam deleted the fix/search-default-relevance branch August 6, 2026 04:33
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.

Search order defaults to Downloads/wk, shows Relevance in dropdown

2 participants