Make use of the indices.

Indices in postgresql rely on operators, so they won't be used if you use only functions.
This commit is contained in:
lain 2019-01-20 00:31:17 +01:00
parent 79e44042bc
commit b108aeee08
2 changed files with 24 additions and 6 deletions

View file

@ -734,7 +734,16 @@ defp fts_search_subquery(query) do
^processed_query
)
},
where: not is_nil(u.nickname)
where:
fragment(
"""
(setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B')) @@ to_tsquery('simple', ?)
""",
u.nickname,
u.name,
^processed_query
)
)
end
@ -750,7 +759,7 @@ defp trigram_search_subquery(query) do
u.name
)
},
where: not is_nil(u.nickname)
where: fragment("trim(? || ' ' || coalesce(?, '')) % ?", u.nickname, u.name, ^query)
)
end

View file

@ -2,12 +2,21 @@ defmodule Pleroma.Repo.Migrations.FixUserTrigramIndex do
use Ecto.Migration
def up do
drop_if_exists index(:users, [], name: :users_trigram_index)
create index(:users, ["(trim(nickname || ' ' || name)) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
drop_if_exists(index(:users, [], name: :users_trigram_index))
create(
index(:users, ["(trim(nickname || ' ' || coalesce(name, ''))) gist_trgm_ops"],
name: :users_trigram_index,
using: :gist
)
)
end
def down do
drop_if_exists index(:users, [], name: :users_trigram_index)
create index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
drop_if_exists(index(:users, [], name: :users_trigram_index))
create(
index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
)
end
end