Fix User search.

Now uses a trigram based search. This is a lot faster and gives better
results. Closes #185.
This commit is contained in:
lain 2018-05-16 17:55:20 +02:00
commit 1d4bbec6b3
5 changed files with 54 additions and 13 deletions

View file

@ -6,3 +6,4 @@ ALTER DATABASE pleroma_dev OWNER TO pleroma;
\c pleroma_dev;
--Extensions made by ecto.migrate that need superuser access
CREATE EXTENSION IF NOT EXISTS citext;
CREATE EXTENSION IF NOT EXISTS pg_trgm;

View file

@ -21,6 +21,7 @@ defmodule Pleroma.User do
field(:local, :boolean, default: true)
field(:info, :map, default: %{})
field(:follower_address, :string)
field(:search_distance, :float, virtual: true)
has_many(:notifications, Notification)
timestamps()
@ -399,19 +400,23 @@ defmodule Pleroma.User do
User.get_or_fetch_by_nickname(query)
end
q =
inner =
from(
u in User,
where:
fragment(
"(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)",
select_merge: %{
search_distance: fragment(
"? <-> (? || ?)",
^query,
u.nickname,
u.name,
^query
),
limit: 20
u.name
)}
)
q = from(s in subquery(inner),
order_by: s.search_distance,
limit: 20
)
Repo.all(q)
end