\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;
field(:local, :boolean, default: true)
field(:info, :map, default: %{})
field(:follower_address, :string)
+ field(:search_distance, :float, virtual: true)
has_many(:notifications, Notification)
timestamps()
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
--- /dev/null
+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
--- /dev/null
+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
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
assert results = json_response(conn, 200)
- [account] = results["accounts"]
+ [account | _] = results["accounts"]
assert account["id"] == to_string(user_three.id)
assert results["hashtags"] == []