Skip to content

fix: keep the autocompleter suggestions in the backend's ranked order - #139

Open
bluzername wants to merge 1 commit into
sahenjp:mainfrom
bluzername:fix/autocomplete-suggestion-order
Open

bluzername wants to merge 1 commit into
sahenjp:mainfrom
bluzername:fix/autocomplete-suggestion-order

Conversation

@bluzername

Copy link
Copy Markdown

What is wrong

`parse_suggestions` in `src/autocomplete.rs` collect the suggestions into a `BTreeSet`. A `BTreeSet` iterate in sorted order, so the final list is alphabetical, not in the order the backend send it. For an autocompleter box this order IS the ranking, this is the whole point of it: DuckDuckGo, Wikipedia and mwmbl all return the suggestion they think is most relevant first.

Example: backend answer `[{"phrase":"zebra"},{"phrase":"apple"}]`, "zebra" is the top ranked one. Current code give the user `["apple", "zebra"]`, so the best match end up last (or dropped off the end once there is more than 10).

The existing tests do not catch this because by coincidence the two fixtures (`"rust"`/`"rust book"` and `"Rust"`/`"Rust (programming language)"`) are already alphabetical (one is a prefix of the other), so the bug stay invisible.

Fix

Swap the `BTreeSet` for a plain `Vec` plus a `HashSet` used only to drop duplicate, insertion order is kept. `suggestions.truncate(10)` replace `.take(10)` on the old sorted iterator, same limit as before.

Added two tests:

  • `suggestion_order_follows_backend_ranking_not_alphabetical_order`: a pair that is NOT already alphabetical (`zebra` before `apple`), fail on the old code, pass on the new one.
  • `duplicate_suggestions_are_removed_without_reordering`: dedup still work and does not touch the order.

The three existing tests keep passing unchanged (their fixtures happen to be alphabetical already, so they do not distinguish the two behaviors, but they still exercise the same code path).

Checks

I could not run `cargo fmt`/`cargo test`/`cargo clippy` here (no execution access to a fresh clone of someone else's repo in this environment), so I am saying it directly instead of claiming a check that did not happen, like CONTRIBUTING.md ask. I read the diff carefully and traced the logic and the new tests by hand against the exact behavior of `BTreeSet` vs `Vec`/`HashSet`. Please run the normal `cargo fmt --all --check`, `cargo test --all-targets`, `cargo clippy --all-targets -- -D warnings` on CI before merge.

@bluzername
bluzername requested a review from sahenjp as a code owner November 14, 2026 05:26
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