discourse/db/migrate/20230201192925_add_trigram_indexes_to_users.rb
Rafael dos Santos Silva 14cf8eacf1
FEATURE: Use similarity in user search (#20112)
Currently, when doing `@mention` for users we have 0 tolerance for typos and misspellings.

With this patch, if a user search doesn't return enough results we go and use `pg_trgm` features to try and find more matches based on trigrams of usernames and names.

It also introduces GiST indexes on those fields in order to improve performance of this search, going from 130ms down to 15ms in my tests.

This is all gated in a feature flag and can be enabled by running  `SiteSetting.user_search_similar_results = true` in the rails console.
2023-02-02 13:35:04 -03:00

25 lines
508 B
Ruby

# frozen_string_literal: true
class AddTrigramIndexesToUsers < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
def change
add_index(
:users,
:username_lower,
using: "gist",
opclass: :gist_trgm_ops,
algorithm: :concurrently,
name: "index_users_on_username_lower_trgm",
)
add_index(
:users,
:name,
using: "gist",
opclass: :gist_trgm_ops,
algorithm: :concurrently,
name: "index_users_on_name_trgm",
)
end
end