Make account search blazing fast and rank followers/followees higher in the results
This commit is contained in:
parent
22f9399cc3
commit
ad0d82d3ce
1
Gemfile
1
Gemfile
|
@ -47,7 +47,6 @@ gem 'rack-attack'
|
||||||
gem 'rack-cors', require: 'rack/cors'
|
gem 'rack-cors', require: 'rack/cors'
|
||||||
gem 'sidekiq'
|
gem 'sidekiq'
|
||||||
gem 'rails-settings-cached'
|
gem 'rails-settings-cached'
|
||||||
gem 'pg_search'
|
|
||||||
gem 'simple-navigation'
|
gem 'simple-navigation'
|
||||||
gem 'statsd-instrument'
|
gem 'statsd-instrument'
|
||||||
gem 'ruby-oembed', require: 'oembed'
|
gem 'ruby-oembed', require: 'oembed'
|
||||||
|
|
|
@ -254,10 +254,6 @@ GEM
|
||||||
parser (2.3.1.2)
|
parser (2.3.1.2)
|
||||||
ast (~> 2.2)
|
ast (~> 2.2)
|
||||||
pg (0.18.4)
|
pg (0.18.4)
|
||||||
pg_search (1.0.6)
|
|
||||||
activerecord (>= 3.1)
|
|
||||||
activesupport (>= 3.1)
|
|
||||||
arel
|
|
||||||
pghero (1.6.2)
|
pghero (1.6.2)
|
||||||
activerecord
|
activerecord
|
||||||
powerpack (0.1.1)
|
powerpack (0.1.1)
|
||||||
|
@ -491,7 +487,6 @@ DEPENDENCIES
|
||||||
paperclip (~> 5.1)
|
paperclip (~> 5.1)
|
||||||
paperclip-av-transcoder
|
paperclip-av-transcoder
|
||||||
pg
|
pg
|
||||||
pg_search
|
|
||||||
pghero
|
pghero
|
||||||
pry-rails
|
pry-rails
|
||||||
puma
|
puma
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
class Account < ApplicationRecord
|
class Account < ApplicationRecord
|
||||||
include Targetable
|
include Targetable
|
||||||
include PgSearch
|
|
||||||
|
|
||||||
MENTION_RE = /(?:^|[^\/\w])@([a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
|
MENTION_RE = /(?:^|[^\/\w])@([a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
|
||||||
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
|
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
|
||||||
|
@ -56,9 +55,6 @@ class Account < ApplicationRecord
|
||||||
# PuSH subscriptions
|
# PuSH subscriptions
|
||||||
has_many :subscriptions, dependent: :destroy
|
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 :remote, -> { where.not(domain: nil) }
|
||||||
scope :local, -> { where(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') }
|
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])
|
Account.find_by_sql([sql, account.id, account.id, limit])
|
||||||
end
|
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)
|
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)
|
follow_mapping(Follow.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class SearchService < BaseService
|
class SearchService < BaseService
|
||||||
def call(query, limit, resolve = false)
|
def call(query, limit, resolve = false, account = nil)
|
||||||
return if query.blank? || query.start_with?('#')
|
return if query.blank? || query.start_with?('#')
|
||||||
|
|
||||||
username, domain = query.gsub(/\A@/, '').split('@')
|
username, domain = query.gsub(/\A@/, '').split('@')
|
||||||
|
@ -9,13 +9,12 @@ class SearchService < BaseService
|
||||||
|
|
||||||
if domain.nil?
|
if domain.nil?
|
||||||
exact_match = Account.find_local(username)
|
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
|
else
|
||||||
exact_match = Account.find_remote(username, domain)
|
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
|
end
|
||||||
|
|
||||||
results = results.limit(limit).to_a
|
|
||||||
results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
|
results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
|
||||||
|
|
||||||
if resolve && !exact_match && !domain.nil?
|
if resolve && !exact_match && !domain.nil?
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
class AddSearchIndexToAccounts < ActiveRecord::Migration[5.0]
|
||||||
|
def up
|
||||||
|
execute 'CREATE INDEX search_index ON accounts USING gin((setweight(to_tsvector(\'simple\', accounts.display_name), \'A\') || setweight(to_tsvector(\'simple\', accounts.username), \'B\') || setweight(to_tsvector(\'simple\', coalesce(accounts.domain, \'\')), \'C\')));'
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
remove_index :accounts, name: :search_index
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20170304202101) do
|
ActiveRecord::Schema.define(version: 20170317193015) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -43,6 +43,7 @@ ActiveRecord::Schema.define(version: 20170304202101) do
|
||||||
t.boolean "silenced", default: false, null: false
|
t.boolean "silenced", default: false, null: false
|
||||||
t.boolean "suspended", default: false, null: false
|
t.boolean "suspended", default: false, null: false
|
||||||
t.boolean "locked", default: false, null: false
|
t.boolean "locked", default: false, null: false
|
||||||
|
t.index "(((setweight(to_tsvector('simple'::regconfig, (display_name)::text), 'A'::\"char\") || setweight(to_tsvector('simple'::regconfig, (username)::text), 'B'::\"char\")) || setweight(to_tsvector('simple'::regconfig, (COALESCE(domain, ''::character varying))::text), 'C'::\"char\")))", name: "search_index", using: :gin
|
||||||
t.index ["username", "domain"], name: "index_accounts_on_username_and_domain", unique: true, using: :btree
|
t.index ["username", "domain"], name: "index_accounts_on_username_and_domain", unique: true, using: :btree
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Reference in a new issue