From c96bdd1766de9bf7cd19808c72c940c268dba7aa Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 7 Sep 2022 16:10:15 -0500 Subject: [PATCH] autocomplete: fix ranking of exact matches. Fix a bug where searching for `sakana~` ranked `sakana~_(meme)` beneath random artist tags containing the word `sakana`. Now, if the search contains punctuation, we rank exact matches first, even for small tags. Before we ranked exact matches for small tags lower than inexact matches for large tags. If the search contains punctuation, it's a strong signal the user is looking for an exact match. --- app/logical/autocomplete_service.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/logical/autocomplete_service.rb b/app/logical/autocomplete_service.rb index 317b030ef..0b0fa4d48 100644 --- a/app/logical/autocomplete_service.rb +++ b/app/logical/autocomplete_service.rb @@ -157,7 +157,14 @@ class AutocompleteService exact = name == string ? 1 : 0 substr = name.include?(string) ? 1 : 0 - [-large, -exact, -substr, -post_count, result[:value]] + # If the search contains punctuation, rank exact matches first then substring matches. Otherwise, if it + # doesn't contain any punctuation, rank exact matches among large tags before small tags. This is so we + # rank exact matches first, but not if they're random small character or artist tags with simple names. + if string.match?(/[^a-zA-Z0-9]/) + [-exact, -substr, -post_count, result[:value]] + else + [-large, -exact, -substr, -post_count, result[:value]] + end end results.take(limit)