FIX: username search logic was stemming and not ordering right
This commit is contained in:
parent
5bcfb6ee38
commit
cf60bed719
|
@ -14,15 +14,12 @@ class UserSearch
|
||||||
|
|
||||||
if @term.present?
|
if @term.present?
|
||||||
if SiteSetting.enable_names?
|
if SiteSetting.enable_names?
|
||||||
users = users.where("username_lower LIKE :term_like OR
|
query = Search.ts_query(@term, "simple")
|
||||||
TO_TSVECTOR('simple', name) @@
|
users = users.includes(:user_search_data)
|
||||||
TO_TSQUERY('simple',
|
.references(:user_search_data)
|
||||||
REGEXP_REPLACE(
|
.where("username_lower LIKE :term_like OR user_search_data.search_data @@ #{query}",
|
||||||
REGEXP_REPLACE(
|
term: @term, term_like: @term_like)
|
||||||
CAST(PLAINTO_TSQUERY(:term) AS TEXT)
|
.order(User.sql_fragment("CASE WHEN username_lower LIKE ? THEN 0 ELSE 1 END ASC", @term_like))
|
||||||
,'\''(?: |$)', ':*''', 'g'),
|
|
||||||
'''', '', 'g')
|
|
||||||
)", term: @term, term_like: @term_like)
|
|
||||||
else
|
else
|
||||||
users = users.where("username_lower LIKE :term_like", term_like: @term_like)
|
users = users.where("username_lower LIKE :term_like", term_like: @term_like)
|
||||||
end
|
end
|
||||||
|
|
|
@ -242,10 +242,11 @@ class Search
|
||||||
self.class.query_locale
|
self.class.query_locale
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.ts_query(term)
|
def self.ts_query(term, locale = nil)
|
||||||
|
locale = Post.sanitize(locale) if locale
|
||||||
all_terms = term.gsub(/[:()&!'"]/,'').split
|
all_terms = term.gsub(/[:()&!'"]/,'').split
|
||||||
query = Post.sanitize(all_terms.map {|t| "#{PG::Connection.escape_string(t)}:*"}.join(" & "))
|
query = Post.sanitize(all_terms.map {|t| "#{PG::Connection.escape_string(t)}:*"}.join(" & "))
|
||||||
"TO_TSQUERY(#{query_locale}, #{query})"
|
"TO_TSQUERY(#{locale || query_locale}, #{query})"
|
||||||
end
|
end
|
||||||
|
|
||||||
def ts_query
|
def ts_query
|
||||||
|
|
|
@ -6,7 +6,7 @@ describe UserSearch do
|
||||||
let(:topic2) { Fabricate :topic }
|
let(:topic2) { Fabricate :topic }
|
||||||
let(:topic3) { Fabricate :topic }
|
let(:topic3) { Fabricate :topic }
|
||||||
let(:user1) { Fabricate :user, username: "mrb", name: "Michael Madsen", last_seen_at: 10.days.ago }
|
let(:user1) { Fabricate :user, username: "mrb", name: "Michael Madsen", last_seen_at: 10.days.ago }
|
||||||
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Bunker", last_seen_at: 9.days.ago }
|
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Code", last_seen_at: 9.days.ago }
|
||||||
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth", last_seen_at: 8.days.ago }
|
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth", last_seen_at: 8.days.ago }
|
||||||
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi", last_seen_at: 7.days.ago }
|
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi", last_seen_at: 7.days.ago }
|
||||||
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino", last_seen_at: 6.days.ago }
|
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino", last_seen_at: 6.days.ago }
|
||||||
|
@ -15,6 +15,8 @@ describe UserSearch do
|
||||||
let(:moderator) { Fabricate :moderator, username: "themod" }
|
let(:moderator) { Fabricate :moderator, username: "themod" }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
ActiveRecord::Base.observers.enable :all
|
||||||
|
|
||||||
Fabricate :post, user: user1, topic: topic
|
Fabricate :post, user: user1, topic: topic
|
||||||
Fabricate :post, user: user2, topic: topic2
|
Fabricate :post, user: user2, topic: topic2
|
||||||
Fabricate :post, user: user3, topic: topic
|
Fabricate :post, user: user3, topic: topic
|
||||||
|
@ -31,6 +33,7 @@ describe UserSearch do
|
||||||
# this is a seriously expensive integration test, re-creating this entire test db is too expensive
|
# this is a seriously expensive integration test, re-creating this entire test db is too expensive
|
||||||
# reuse
|
# reuse
|
||||||
it "operates correctly" do
|
it "operates correctly" do
|
||||||
|
|
||||||
# normal search
|
# normal search
|
||||||
results = search_for(user1.name.split(" ").first)
|
results = search_for(user1.name.split(" ").first)
|
||||||
results.size.should == 1
|
results.size.should == 1
|
||||||
|
@ -89,6 +92,12 @@ describe UserSearch do
|
||||||
results = search_for("Tarantino")
|
results = search_for("Tarantino")
|
||||||
results.size.should == 1
|
results.size.should == 1
|
||||||
|
|
||||||
|
results = search_for("coding")
|
||||||
|
results.size.should == 0
|
||||||
|
|
||||||
|
results = search_for("z")
|
||||||
|
results.size.should == 0
|
||||||
|
|
||||||
# When searching by name is disabled, it will not return the record
|
# When searching by name is disabled, it will not return the record
|
||||||
SiteSetting.enable_names = false
|
SiteSetting.enable_names = false
|
||||||
results = search_for("Tarantino")
|
results = search_for("Tarantino")
|
||||||
|
@ -99,6 +108,7 @@ describe UserSearch do
|
||||||
results = search_for("mrB")
|
results = search_for("mrB")
|
||||||
results.first.should == user1
|
results.first.should == user1
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue