Commit 1d4bbec6 by lain

Fix User search.

Now uses a trigram based search. This is a lot faster and gives better results. Closes #185.
parent 2e9aa16b
Pipeline #1817 failed with stages
in 2 minutes 20 seconds
......@@ -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;
......@@ -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
......
defmodule Pleroma.Repo.Migrations.AddTrigramExtension do
use Ecto.Migration
require Logger
def up do
Logger.warn("ATTENTION ATTENTION ATTENTION\n")
Logger.warn("This will try to create the pg_trgm extension on your database. If your database user does NOT have the necessary rights, you will have to do it manually and re-run the migrations.\nYou can probably do this by running the following:\n")
Logger.warn("sudo -u postgres psql pleroma_dev -c \"create extension if not exists pg_trgm\"\n")
execute("create extension if not exists pg_trgm")
end
def down do
execute("drop extension if exists pg_trgm")
end
end
defmodule Pleroma.Repo.Migrations.CreateUserTrigramIndex do
use Ecto.Migration
def change do
create index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
end
end
......@@ -609,16 +609,29 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
test "account search", %{conn: conn} do
user = insert(:user)
_user_two = insert(:user, %{nickname: "shp@shitposter.club"})
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
conn =
results =
conn
|> assign(:user, user)
|> get("/api/v1/accounts/search", %{"q" => "shp"})
|> json_response(200)
result_ids = for result <- results, do: result["acct"]
assert user_two.nickname in result_ids
assert user_three.nickname in result_ids
results =
conn
|> assign(:user, user)
|> get("/api/v1/accounts/search", %{"q" => "2hu"})
|> json_response(200)
assert [account] = json_response(conn, 200)
assert account["id"] == to_string(user_three.id)
result_ids = for result <- results, do: result["acct"]
assert user_three.nickname in result_ids
end
test "search", %{conn: conn} do
......@@ -642,7 +655,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
assert results = json_response(conn, 200)
[account] = results["accounts"]
[account | _] = results["accounts"]
assert account["id"] == to_string(user_three.id)
assert results["hashtags"] == []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment