Autocomplete is not showing an exact match at the top

Created on 16 August 2022, over 2 years ago
Updated 29 August 2023, over 1 year ago

Problem/Motivation

When typing a content title into a link field, Drupal returns a list of 10 items that contain that phrase anywhere in the content title, sorted by creation date.

The bug happens when:

  • you are searching for a node with an older creation date,
  • you type the exact node title in the link field, and
  • Drupal returns 10 newer pieces of content whose titles include that phrase.

Since the node you are searching for has an older creation date than the 10 results Drupal returns, there is no way to get Drupal to show the result that exactly matches what is typed into the link field.

For example, I was looking for the "Science Education" node, but I saw a lot of results that had "Science Education" as part of the title, but the node with an exact title ("Science Education") was omitted from autocomplete list, because the list was limited to 10 items and did not include the exact match expected to be shown first. See screenshot.

This issue may be an issue of any Drupal autocomplete field that reuses core functionality for the autocomplete. The link field implementation uses default settings of that autocomplete functionality.

Steps to reproduce

1. Create one node whose title is exactly "About".
2. Create another 10 nodes containing "about" somewhere in the title.
3. Add a link field to one of Drupal's content types, make this link both external and internal.
4. Create a new node that is the same content type as step #3.
6. In the link field, type "about".
7. Check autocomplete values in the list, the "About" node (the one you created in the 1st step) is omitted. (Because it would be the 11th item when sorted by creation date.)

Proposed resolution

Sort the link field's autocomplete results by title length (shorter titles first) before limiting the results to 10 items. This would ensure that exact title matches are always first and long node titles will eventually be found by typing more characters into the link field.

Something like:

SELECT * FROM `node` WHERE title LIKE '%about%' ORDER BY length(title) ASC;

See https://stackoverflow.com/questions/31315669/how-to-use-mysql-like-with-...

User interface changes

Instead of the link field's autocomplete showing a list of the 10 most recent nodes that match, the autocomplete list will show the 10 shortest titles of nodes that match.

Original report by dealancer

This bug happens in the link widget. And yes this is a bug, not a feature ;-)

When there is many content on the website and someone is searching for "some phrase" among existing content, Drupal does not give a preference to the content with an exact match. In other words, a node with "some phrase" title is not even showing in the list, but in this list there could be other content items with a title like "prefix some phrase suffix" that contain "some phrase", but they are not an exact match.

In the given example I was looking up for "Science Education" node, but I saw a lot of results that has "Science Education" as part of the title, but the node with an exact title ("Science Education") was omitted from autocomplete list, because a list was limited by 10 items only. However the problem is not in the length of the list, but in the search query as the exact match expected to be shown first. See screenshot.

This issue may be an issue of any Drupal autocomplete field that reuses core functionality for the autocomplete. The link field implementation uses default settings of that autocomplete functionality.

Proposed resolution

There are could be several solutions to this issue, but all of them would be altering SQL to add the order option, it could look something like this:

SELECT * FROM `table_name` WHERE col LIKE '%ami%' order by (col LIKE 'ami%') desc;

See https://stackoverflow.com/questions/31315669/how-to-use-mysql-like-with-... for more ways.

πŸ› Bug report
Status

Active

Version

9.5

Component
AjaxΒ  β†’

Last updated 2 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States dealancer Washington D.C.

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΉπŸ‡ΌTaiwan johnalbin Taipei, Taiwan

    This bit me recently. It appears the autocomplete returns the 10 most recent nodes ordered by creation date. So if you created an "About" page a long time ago and then later created 10+ nodes that included the word "about" somewhere in the title, you can no longer see the "About" page in the autocomplete list when typing "About" in the link field. And because you've already typed the exact node title you want, there's no way to make Drupal return that result.

    I think Jason β†’ ’s solution of sorting by title length is brilliant! Because it means you will always be able to find the node title you want by typing more of the title in the autocomplete.

    Here's an example:

    1. I type "about" in a link field and Drupal returns 10 items sorted by title length:
      • About
      • About me
      • About CSS
      • …
      • About Drupal
    2. I examine the list to see if what I want is there.
      • If what I want is exactly "About", then it's the first option!
      • If what I want is longer than "About Drupal", it's not in that list and I'll have to type more letters into the link field.
    3. I didn't want anything in that first list, so I type "about my" and Drupal returns a new list of 10 items sorted by title length:
      • About my job
      • About my dog
      • About my team
      • About my dreams
      • About my team's dogs
      • …
      • About Mycenaean Greece
    4. I repeat step 2, type "about my dog", and see the desired title, "about my dog's favorite chew toy".

    The only problem this solution doesn't solve is if you have 11 nodes all named "About" exactly, but I think that's a content problem, not one Drupal needs to account for.

    I've updated the issue summary to show the proposed solution and (hopefully) to make it easier to understand the problem.

  • πŸ‡ΉπŸ‡ΌTaiwan johnalbin Taipei, Taiwan

    Oh! I forgot to mention this happens when editing menu links too. So it doesn't happen in just the link field's autocomplete.

  • πŸ‡ΉπŸ‡ΌTaiwan johnalbin Taipei, Taiwan
  • πŸ‡ΉπŸ‡ΌTaiwan johnalbin Taipei, Taiwan
  • πŸ‡¬πŸ‡§United Kingdom lincoln-batsirayi

    The hook on #3 by @matthiasm11 has worked for me, a good placeholder until a more firm decision has been made on this issue.

  • πŸ‡¨πŸ‡ΏCzech Republic parisek

    We have same issue and #3 fixed it for us, thank you

  • πŸ‡¨πŸ‡¦Canada phjou Vancouver πŸ‡¨πŸ‡¦ πŸ‡ͺπŸ‡Ί

    Wa had the same issue, hook #3 seems to do the trick, thanks!

Production build 0.71.5 2024