[Search 2.0] Allow mention autocomplete to search users names (#13462)
* Add tsvector index on users.name * [Search 2.0] Allow mention autocomplete to search users names
This commit is contained in:
parent
a615105653
commit
1fd38aa172
6 changed files with 45 additions and 7 deletions
|
|
@ -6,8 +6,8 @@ class User < ApplicationRecord
|
|||
include Storext.model
|
||||
|
||||
include PgSearch::Model
|
||||
pg_search_scope :search_by_username,
|
||||
against: :username,
|
||||
pg_search_scope :search_by_name_and_username,
|
||||
against: %i[name username],
|
||||
using: { tsearch: { prefix: true } }
|
||||
|
||||
# @citizen428 Preparing to drop profile columns from the users table
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ module Search
|
|||
].freeze
|
||||
|
||||
def self.search_documents(term)
|
||||
results = ::User.search_by_username(term).limit(MAX_RESULTS).select(*ATTRIBUTES)
|
||||
results = ::User.search_by_name_and_username(term).limit(MAX_RESULTS).select(*ATTRIBUTES)
|
||||
|
||||
serialize(results)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
class AddNameTsvectorIndexToUsers < ActiveRecord::Migration[6.1]
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
add_index(
|
||||
:users,
|
||||
"to_tsvector('simple'::regconfig, COALESCE((name)::text, ''::text))",
|
||||
using: :gin,
|
||||
name: :index_users_on_name_as_tsvector,
|
||||
algorithm: :concurrently
|
||||
)
|
||||
end
|
||||
|
||||
def down
|
||||
remove_index :users, name: :index_users_on_name_as_tsvector, algorithm: :concurrently, if_exists: true
|
||||
end
|
||||
end
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2021_04_14_033457) do
|
||||
ActiveRecord::Schema.define(version: 2021_04_20_135208) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "citext"
|
||||
|
|
@ -1358,6 +1358,7 @@ ActiveRecord::Schema.define(version: 2021_04_14_033457) do
|
|||
t.boolean "welcome_notifications", default: true, null: false
|
||||
t.datetime "workshop_expiration"
|
||||
t.string "youtube_url"
|
||||
t.index "to_tsvector('simple'::regconfig, COALESCE((name)::text, ''::text))", name: "index_users_on_name_as_tsvector", using: :gin
|
||||
t.index "to_tsvector('simple'::regconfig, COALESCE((username)::text, ''::text))", name: "index_users_on_username_as_tsvector", using: :gin
|
||||
t.index ["apple_username"], name: "index_users_on_apple_username"
|
||||
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
|
||||
|
|
|
|||
|
|
@ -140,7 +140,7 @@ RSpec.describe "Search", type: :request, proper_status: true do
|
|||
allow(Search::User).to receive(:search_usernames).and_return(
|
||||
result,
|
||||
)
|
||||
get "/search/usernames"
|
||||
get search_usernames_path
|
||||
expect(response.parsed_body).to eq("result" => result)
|
||||
end
|
||||
end
|
||||
|
|
@ -155,13 +155,21 @@ RSpec.describe "Search", type: :request, proper_status: true do
|
|||
expect(response.parsed_body["result"]).to be_empty
|
||||
end
|
||||
|
||||
it "finds a username by a partial name" do
|
||||
it "finds a username by a partial username" do
|
||||
user = create(:user, username: "Sloan")
|
||||
|
||||
get search_usernames_path(username: "slo")
|
||||
|
||||
expect(response.parsed_body["result"].first).to include("username" => user.username)
|
||||
end
|
||||
|
||||
it "finds a username by a partial name" do
|
||||
user = create(:user, name: "Sloan")
|
||||
|
||||
get search_usernames_path(username: "slo")
|
||||
|
||||
expect(response.parsed_body["result"].first).to include("username" => user.username)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -29,9 +29,21 @@ RSpec.describe Search::Postgres::Username, type: :service do
|
|||
expect(described_class.search_documents(user.username.first(1))).to be_present
|
||||
end
|
||||
|
||||
it "finds a user by their name" do
|
||||
user = create(:user)
|
||||
|
||||
expect(described_class.search_documents(user.name)).to be_present
|
||||
end
|
||||
|
||||
it "finds a user by a partial name" do
|
||||
user = create(:user)
|
||||
|
||||
expect(described_class.search_documents(user.name.first(3))).to be_present
|
||||
end
|
||||
|
||||
it "finds multiple users whose names have common parts", :aggregate_failures do
|
||||
alex = create(:user, username: "alex")
|
||||
alexsmith = create(:user, username: "alexsmith")
|
||||
alexsmith = create(:user, name: "alexsmith")
|
||||
rhymes = create(:user, username: "rhymes")
|
||||
|
||||
result = described_class.search_documents("ale")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue