0
0
Fork 0

Search cleanup (#1333)

* Clean up SQL output in Tag and Account search methods

* Add basic coverage for Tag.search_for

* Add coverage for Account.search_for

* Add coverage for Account.advanced_search_for
This commit is contained in:
Matt Jankowski 2017-04-09 08:45:01 -04:00 committed by Eugen
parent 71706f21c2
commit 388ec0d5b6
4 changed files with 74 additions and 8 deletions

View file

@ -203,7 +203,7 @@ class Account < ApplicationRecord
end
def triadic_closures(account, limit = 5)
sql = <<SQL
sql = <<-SQL.squish
WITH first_degree AS (
SELECT target_account_id
FROM follows
@ -216,7 +216,7 @@ class Account < ApplicationRecord
GROUP BY target_account_id, accounts.id
ORDER BY count(account_id) DESC
LIMIT ?
SQL
SQL
Account.find_by_sql([sql, account.id, account.id, limit])
end
@ -226,7 +226,7 @@ SQL
textsearch = '(setweight(to_tsvector(\'simple\', accounts.display_name), \'A\') || setweight(to_tsvector(\'simple\', accounts.username), \'B\') || setweight(to_tsvector(\'simple\', coalesce(accounts.domain, \'\')), \'C\'))'
query = 'to_tsquery(\'simple\', \'\'\' \' || ' + terms + ' || \' \'\'\' || \':*\')'
sql = <<SQL
sql = <<-SQL.squish
SELECT
accounts.*,
ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
@ -234,7 +234,7 @@ SQL
WHERE #{query} @@ #{textsearch}
ORDER BY rank DESC
LIMIT ?
SQL
SQL
Account.find_by_sql([sql, limit])
end
@ -244,7 +244,7 @@ SQL
textsearch = '(setweight(to_tsvector(\'simple\', accounts.display_name), \'A\') || setweight(to_tsvector(\'simple\', accounts.username), \'B\') || setweight(to_tsvector(\'simple\', coalesce(accounts.domain, \'\')), \'C\'))'
query = 'to_tsquery(\'simple\', \'\'\' \' || ' + terms + ' || \' \'\'\' || \':*\')'
sql = <<SQL
sql = <<-SQL.squish
SELECT
accounts.*,
(count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
@ -254,7 +254,7 @@ SQL
GROUP BY accounts.id
ORDER BY rank DESC
LIMIT ?
SQL
SQL
Account.find_by_sql([sql, account.id, account.id, limit])
end