From ad0d82d3cee27839371b3182b434d8e78092890a Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 17 Mar 2017 20:47:38 +0100 Subject: [PATCH] Make account search blazing fast and rank followers/followees higher in the results --- Gemfile | 1 - Gemfile.lock | 5 --- app/models/account.rb | 40 +++++++++++++++++-- app/services/search_service.rb | 7 ++-- ...0317193015_add_search_index_to_accounts.rb | 9 +++++ db/schema.rb | 3 +- 6 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20170317193015_add_search_index_to_accounts.rb diff --git a/Gemfile b/Gemfile index c97f80bde..56a8a6594 100644 --- a/Gemfile +++ b/Gemfile @@ -47,7 +47,6 @@ gem 'rack-attack' gem 'rack-cors', require: 'rack/cors' gem 'sidekiq' gem 'rails-settings-cached' -gem 'pg_search' gem 'simple-navigation' gem 'statsd-instrument' gem 'ruby-oembed', require: 'oembed' diff --git a/Gemfile.lock b/Gemfile.lock index 8ab50773f..c8a5991d0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -254,10 +254,6 @@ GEM parser (2.3.1.2) ast (~> 2.2) pg (0.18.4) - pg_search (1.0.6) - activerecord (>= 3.1) - activesupport (>= 3.1) - arel pghero (1.6.2) activerecord powerpack (0.1.1) @@ -491,7 +487,6 @@ DEPENDENCIES paperclip (~> 5.1) paperclip-av-transcoder pg - pg_search pghero pry-rails puma 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 = <