about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/models/account.rb40
-rw-r--r--app/services/search_service.rb7
2 files changed, 39 insertions, 8 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 1eb886ee3..978dc2d71 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -2,7 +2,6 @@
 
 class Account < ApplicationRecord
   include Targetable
-  include PgSearch
 
   MENTION_RE = /(?:^|[^\/\w])@([a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
   IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
@@ -56,9 +55,6 @@ class Account < ApplicationRecord
   # PuSH subscriptions
   has_many :subscriptions, dependent: :destroy
 
-  pg_search_scope :search_for, against: { display_name: 'A', username: 'B', domain: 'C' },
-                               using: { tsearch: { prefix: true } }
-
   scope :remote, -> { where.not(domain: nil) }
   scope :local, -> { where(domain: nil) }
   scope :without_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) = 0') }
@@ -212,6 +208,42 @@ SQL
       Account.find_by_sql([sql, account.id, account.id, limit])
     end
 
+    def search_for(terms, limit = 10)
+      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\', \'\'\' \' || ? || \' \'\'\' || \':*\')'
+
+      sql = <<SQL
+        SELECT
+          accounts.*,
+          ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
+        FROM accounts
+        WHERE #{query} @@ #{textsearch}
+        ORDER BY rank DESC
+        LIMIT ?
+SQL
+
+      Account.find_by_sql([sql, terms, terms, limit])
+    end
+
+    def advanced_search_for(terms, account, limit = 10)
+      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\', \'\'\' \' || ? || \' \'\'\' || \':*\')'
+
+      sql = <<SQL
+        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}
+        GROUP BY accounts.id
+        ORDER BY rank DESC
+        LIMIT ?
+SQL
+
+      Account.find_by_sql([sql, terms, account.id, account.id, terms, limit])
+    end
+
     def following_map(target_account_ids, account_id)
       follow_mapping(Follow.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
     end
diff --git a/app/services/search_service.rb b/app/services/search_service.rb
index 6f740e149..19fc16973 100644
--- a/app/services/search_service.rb
+++ b/app/services/search_service.rb
@@ -1,7 +1,7 @@
 # frozen_string_literal: true
 
 class SearchService < BaseService
-  def call(query, limit, resolve = false)
+  def call(query, limit, resolve = false, account = nil)
     return if query.blank? || query.start_with?('#')
 
     username, domain = query.gsub(/\A@/, '').split('@')
@@ -9,13 +9,12 @@ class SearchService < BaseService
 
     if domain.nil?
       exact_match = Account.find_local(username)
-      results     = Account.search_for(username)
+      results     = account.nil? ? Account.search_for(username, limit) : Account.advanced_search_for(username, account, limit)
     else
       exact_match = Account.find_remote(username, domain)
-      results     = Account.search_for("#{username} #{domain}")
+      results     = account.nil? ? Account.search_for("#{username} #{domain}", limit) : Account.advanced_search_for("#{username} #{domain}", account, limit)
     end
 
-    results = results.limit(limit).to_a
     results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
 
     if resolve && !exact_match && !domain.nil?