diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-11-24 12:24:24 +0100 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-11-27 11:29:40 +0100 |
commit | da42dfb3cf4a2fb0cdcc1a3b41438516a0bed0e5 (patch) | |
tree | 1dc5014e27ce049e47e8670b83b79e0c49bb96b9 /app/models/user.rb | |
parent | d4eea275310867eccc927d0e92a1d19a165f0668 (diff) | |
download | gitlab-ce-dm-search-pattern.tar.gz |
Use fuzzy search with minimum length of 3 characters where appropriatedm-search-pattern
Diffstat (limited to 'app/models/user.rb')
-rw-r--r-- | app/models/user.rb | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 9a35336c574..14941fd7f98 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -313,9 +313,6 @@ class User < ActiveRecord::Base # # Returns an ActiveRecord::Relation. def search(query) - table = arel_table - pattern = User.to_pattern(query) - order = <<~SQL CASE WHEN users.name = %{query} THEN 0 @@ -325,11 +322,8 @@ class User < ActiveRecord::Base END SQL - where( - table[:name].matches(pattern) - .or(table[:email].matches(pattern)) - .or(table[:username].matches(pattern)) - ).reorder(order % { query: ActiveRecord::Base.connection.quote(query) }, :name) + fuzzy_search(query, [:name, :email, :username]) + .reorder(order % { query: ActiveRecord::Base.connection.quote(query) }, :name) end # searches user by given pattern @@ -337,16 +331,16 @@ class User < ActiveRecord::Base # This method uses ILIKE on PostgreSQL and LIKE on MySQL. def search_with_secondary_emails(query) - table = arel_table email_table = Email.arel_table - pattern = to_pattern(query) - matched_by_emails_user_ids = email_table.project(email_table[:user_id]).where(email_table[:email].matches(pattern)) + matched_by_emails_user_ids = email_table + .project(email_table[:user_id]) + .where(Email.fuzzy_arel_match(:email, query)) where( - table[:name].matches(pattern) - .or(table[:email].matches(pattern)) - .or(table[:username].matches(pattern)) - .or(table[:id].in(matched_by_emails_user_ids)) + fuzzy_arel_match(:name, query) + .or(fuzzy_arel_match(:email, query)) + .or(fuzzy_arel_match(:username, query)) + .or(arel_table[:id].in(matched_by_emails_user_ids)) ) end |