aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2018-05-16 17:55:20 +0200
committerlain <lain@soykaf.club>2018-05-16 17:55:20 +0200
commit1d4bbec6b3239bb83b500a6a90e6686cb682cfac (patch)
tree784685762399e4e2d2e630a2f20ece05b91b6809 /lib
parent2e9aa16b862fac043a5c1a538fc6eacdaa7ec56f (diff)
downloadpleroma-1d4bbec6b3239bb83b500a6a90e6686cb682cfac.tar.gz
Fix User search.
Now uses a trigram based search. This is a lot faster and gives better results. Closes #185.
Diffstat (limited to 'lib')
-rw-r--r--lib/mix/tasks/sample_psql.eex1
-rw-r--r--lib/pleroma/user.ex21
2 files changed, 14 insertions, 8 deletions
diff --git a/lib/mix/tasks/sample_psql.eex b/lib/mix/tasks/sample_psql.eex
index 18e322efc..bc22f166c 100644
--- a/lib/mix/tasks/sample_psql.eex
+++ b/lib/mix/tasks/sample_psql.eex
@@ -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;
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 207674999..399a66787 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -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