Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/models/concerns/user_fuzzy_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def fuzzy_ranked_search(term, limit: 20)
candidate_parts = [
"SELECT id FROM users WHERE slack_uid = :exact",
"SELECT id FROM users WHERE username ILIKE :contains",
"SELECT id FROM users WHERE display_name_override ILIKE :contains",
Comment thread
skyfallwastaken marked this conversation as resolved.
"SELECT id FROM users WHERE github_username ILIKE :contains",
"SELECT id FROM users WHERE slack_username ILIKE :contains",
"SELECT user_id AS id FROM email_addresses WHERE email ILIKE :contains"
Expand All @@ -33,6 +34,10 @@ def fuzzy_ranked_search(term, limit: 20)
WHEN users.username ILIKE :prefix THEN 50
WHEN users.username ILIKE :contains THEN 10
ELSE 0 END +
CASE WHEN users.display_name_override ILIKE :ilike_exact THEN 100
WHEN users.display_name_override ILIKE :prefix THEN 50
WHEN users.display_name_override ILIKE :contains THEN 10
ELSE 0 END +
CASE WHEN users.github_username ILIKE :ilike_exact THEN 100
WHEN users.github_username ILIKE :prefix THEN 50
WHEN users.github_username ILIKE :contains THEN 10
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class AddTrgmIndexToUsersDisplayNameOverride < ActiveRecord::Migration[8.1]
disable_ddl_transaction!

def up
enable_extension :pg_trgm unless extension_enabled?(:pg_trgm)

add_index :users, :display_name_override,
name: :index_users_on_display_name_override_trgm,
using: :gin,
opclass: :gin_trgm_ops,
algorithm: :concurrently,
if_not_exists: true
end

def down
remove_index :users, :display_name_override,
name: :index_users_on_display_name_override_trgm,
algorithm: :concurrently,
if_exists: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/models/concerns/user_fuzzy_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# - rank scoring tiers (id/slack_uid exact = 1000; field exact = 100;
# prefix = 50; contains = 10; tiers compound additively)
# - case-insensitive ILIKE on username/email fields
# - display name overrides are searchable and ranked like other name fields
# - case-sensitive equality on slack_uid
# - matched_email picks best email (exact > prefix > contains > any)
# - matched_email is nil only when the user has zero emails
Expand Down Expand Up @@ -114,6 +115,16 @@ def with_email(user, email)
assert User.fuzzy_ranked_search("MIXEDCASEUSER").any? { |r| r.id == u.id }
end

test "display name override is searchable and ranked case-insensitively" do
u = create_user(username: "unrelated_username", display_name_override: "Custom Display Name")

exact = User.fuzzy_ranked_search("custom display name").find { |r| r.id == u.id }
substring = User.fuzzy_ranked_search("DISPLAY").find { |r| r.id == u.id }

assert_operator exact.rank_score, :>=, 100
assert_operator substring.rank_score, :>=, 10
end

# ----- multi-field tiers -----

test "github_username, slack_username, and email tiers contribute" do
Expand Down