Commit e59a0922 authored by Hordur Freyr Yngvason's avatar Hordur Freyr Yngvason Committed by Steve Abrams

Use lowercase values in user search ordering

Fixes a subtle bug in the user search ranking where a case-sensitive
match (PostgreSQL string equality) was used on a value that had
previously been converted to lowercase. As a result, exact matches could
receive the lowest priority instead of the higest priority.

For distinctive usernames, this was not a problem, as those would result
in unique matches anyway. However, when the username matched a prefix of
a common human name, an exact match could get buried at the bottom of
the results.

For example, since Alexander is a very common name, this made it
virtually impossible to find @Alexand in most GitLab dropdowns.

Changelog: fixed
parent dda57adf
......@@ -663,9 +663,9 @@ class User < ApplicationRecord
order = <<~SQL
CASE
WHEN users.name = :query THEN 0
WHEN users.username = :query THEN 1
WHEN users.public_email = :query THEN 2
WHEN LOWER(users.name) = :query THEN 0
WHEN LOWER(users.username) = :query THEN 1
WHEN LOWER(users.public_email) = :query THEN 2
ELSE 3
END
SQL
......
......@@ -2653,6 +2653,19 @@ RSpec.describe User do
let_it_be(:user3) { create(:user, name: 'us', username: 'se', email: 'foo@example.com') }
let_it_be(:email) { create(:email, user: user, email: 'alias@example.com') }
describe 'name user and email relative ordering' do
let_it_be(:named_alexander) { create(:user, name: 'Alexander Person', username: 'abcd', email: 'abcd@example.com') }
let_it_be(:username_alexand) { create(:user, name: 'Joao Alexander', username: 'Alexand', email: 'joao@example.com') }
it 'prioritizes exact matches' do
expect(described_class.search('Alexand')).to eq([username_alexand, named_alexander])
end
it 'falls back to ordering by name' do
expect(described_class.search('Alexander')).to eq([named_alexander, username_alexand])
end
end
describe 'name matching' do
it 'returns users with a matching name with exact match first' do
expect(described_class.search(user.name)).to eq([user, user2])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment