alphagov / accessible-autocomplete

An autocomplete component, built to be accessible.
https://alphagov.github.io/accessible-autocomplete/examples/
MIT License
919 stars 149 forks source link

Results should be filtered on closest matches, rather than sorted alphabetically #326

Open alexnewmannn opened 5 years ago

alexnewmannn commented 5 years ago

given i have a list of friend, boyfriend, girlfriend if i type in friend i should see friend, boyfriend, girlfriend in that order, however, I will see boyfriend, friend, girlfriend as the results are alphabetised. If the start of the search matches the start of one of the results that should be prioritised.

Similarly, given a list of husband, daughter if i type in h the order of results will be daughter, husband where I'd expect it to be husband, daughter

edwardhorsford commented 5 years ago

I wrote a weighting algorithm for the country and territory picker - the intention was that it would get ported to the autocomplete - but it doesn't look like that happened.

The rough idea was to prioritise in this order: exact match, match to start of a word, match to any part of a word. We then weighted matches against the 'canonical' name higher than synonyms.

I was really pleased with how it worked when we tested it. It could probably be rewritten by a developer though.

It also supported each entry having a default weighting - so an entry that is likely to be much more common can be given a weight of 2 - which will double the score and place it higher than others of a similar match.

alexnewmannn commented 5 years ago

@edwardhorsford Thanks, I'll take a look at this. It may be that we take this approach short term, but definitely agree that this and aliases should exist here as well as the country picker.

frankieroberto commented 5 years ago

Here's the basic weighting algorithm I used in the govuk-government-organisations autocomplete (which has been used by a couple of different teams): https://github.com/frankieroberto/govuk-government-organisations-autocomplete/blob/master/src/govuk_government_organisations_autocomplete.js#L27-L83

The intention was to prioritise start-of-word matches (eg "ed" matching "Department of Education") as well as abbreviation matches (eg "dwp" for "Department for Work and Pensions"). Central government departments are prioritised over agencies other organisations through sorting the original list (which is used in the case of a tie).

ediblecode commented 5 years ago

If anyone's interested, I wrote a little 'suggestion engine' for use with Accessible Autocomplete. It matches both terms and phrases and then ranks based on phrases being preferable over terms, and prefers matches closer to the start of the string etc. Anyway, might help someone: https://github.com/nhsevidence/global-nav/blob/master/src/Header/Search/Autocomplete/suggester/index.js.

Ideally it would be nice to add some extra bits like stemming or synonyms etc but we didn't need that. Might move it into its own repo if anyone's interested.