[#477] Added FTS index for `users`. Fixed failing test.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 15 Jan 2019 09:04:54 +0000 (12:04 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 15 Jan 2019 09:04:54 +0000 (12:04 +0300)
priv/repo/migrations/20190115085500_create_user_fts_index.exs [new file with mode: 0644]
test/web/twitter_api/twitter_api_controller_test.exs

diff --git a/priv/repo/migrations/20190115085500_create_user_fts_index.exs b/priv/repo/migrations/20190115085500_create_user_fts_index.exs
new file mode 100644 (file)
index 0000000..499d671
--- /dev/null
@@ -0,0 +1,17 @@
+defmodule Pleroma.Repo.Migrations.CreateUserFtsIndex do
+  use Ecto.Migration
+
+  def change do
+    create index(
+             :users,
+             [
+               """
+               (setweight(to_tsvector('simple', regexp_replace(nickname, '\\W', ' ', 'g')), 'A') ||
+               setweight(to_tsvector('simple', regexp_replace(coalesce(name, ''), '\\W', ' ', 'g')), 'B'))
+               """
+             ],
+             name: :users_fts_index,
+             using: :gin
+           )
+  end
+end
index 5f13e79595b84479cdf40b66b78ac264cc99e8c5..a4baf2b5ffd5122c692de17fe5d1921f87e2749c 100644 (file)
@@ -1655,16 +1655,16 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   describe "GET /api/pleroma/search_user" do
     test "it returns users, ordered by similarity", %{conn: conn} do
       user = insert(:user, %{name: "eal"})
-      user_two = insert(:user, %{name: "ean"})
-      user_three = insert(:user, %{name: "ebn"})
+      user_two = insert(:user, %{name: "eal me"})
+      _user_three = insert(:user, %{name: "ebn"})
 
       resp =
         conn
-        |> get(twitter_api_search__path(conn, :search_user), query: "eal")
+        |> get(twitter_api_search__path(conn, :search_user), query: "eal me")
         |> json_response(200)
 
-      assert length(resp) == 3
-      assert [user.id, user_two.id, user_three.id] == Enum.map(resp, fn %{"id" => id} -> id end)
+      assert length(resp) == 2
+      assert [user_two.id, user.id] == Enum.map(resp, fn %{"id" => id} -> id end)
     end
   end