Updated account search result sorting

• Updated:
- search to order results for users by follow count, rank and if they're verified or not
This commit is contained in:
mgabdev 2020-06-12 21:45:35 -04:00
parent 69908f629a
commit 79330acb21

View File

@ -417,7 +417,7 @@ class Account < ApplicationRecord
AND accounts.suspended_at IS NULL AND accounts.suspended_at IS NULL
AND accounts.moved_to_account_id IS NULL AND accounts.moved_to_account_id IS NULL
AND accounts.domain IS NULL AND accounts.domain IS NULL
ORDER BY rank DESC ORDER BY accounts.is_verified DESC
LIMIT ? OFFSET ? LIMIT ? OFFSET ?
SQL SQL
@ -429,47 +429,24 @@ class Account < ApplicationRecord
def advanced_search_for(terms, account, limit = 10, following = false, offset = 0) def advanced_search_for(terms, account, limit = 10, following = false, offset = 0)
textsearch, query = generate_query_for_search(terms) textsearch, query = generate_query_for_search(terms)
if following sql = <<-SQL.squish
sql = <<-SQL.squish SELECT
WITH first_degree AS ( accounts.*,
SELECT target_account_id (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank,
FROM follows (count(f.id) + 1) AS fc
WHERE account_id = ? FROM accounts
) LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
SELECT WHERE #{query} @@ #{textsearch}
accounts.*, AND accounts.suspended_at IS NULL
(count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank AND accounts.moved_to_account_id IS NULL
FROM accounts AND accounts.domain IS NULL
LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?) GROUP BY accounts.id
WHERE accounts.id IN (SELECT * FROM first_degree) ORDER BY accounts.is_verified DESC, fc DESC, rank DESC
AND #{query} @@ #{textsearch} LIMIT ? OFFSET ?
AND accounts.suspended_at IS NULL SQL
AND accounts.moved_to_account_id IS NULL
AND accounts.domain IS NULL
GROUP BY accounts.id
ORDER BY rank DESC
LIMIT ? OFFSET ?
SQL
records = find_by_sql([sql, account.id, account.id, account.id, limit, offset]) records = find_by_sql([sql, account.id, account.id, limit, offset])
else
sql = <<-SQL.squish
SELECT
accounts.*,
(count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
FROM accounts
LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
WHERE #{query} @@ #{textsearch}
AND accounts.suspended_at IS NULL
AND accounts.moved_to_account_id IS NULL
AND accounts.domain IS NULL
GROUP BY accounts.id
ORDER BY rank DESC
LIMIT ? OFFSET ?
SQL
records = find_by_sql([sql, account.id, account.id, limit, offset])
end
ActiveRecord::Associations::Preloader.new.preload(records, :account_stat) ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
records records