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.
This commit is contained in:
evazion
2022-09-07 16:10:15 -05:00
parent 0cc76625eb
commit c96bdd1766

View File

@@ -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)