summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Suprunenko <suprunenko.s@gmail.com>2020-03-31 19:09:34 +0200
committerSergey Suprunenko <suprunenko.s@gmail.com>2020-03-31 19:09:34 +0200
commit72ef8bc7b08540aed95466e3fce36a682fb00c7a (patch)
tree69100ab6eade0377962a70e51bcc762e29e7ec34
parent8bd5ad8664479b722e6905306f6342f35e2a84d2 (diff)
Add activity summary to GIN full text search
-rw-r--r--lib/pleroma/activity/search.ex8
-rw-r--r--priv/repo/migrations/20200327212356_add_summary_to_fts_index.exs12
-rw-r--r--test/web/mastodon_api/controllers/search_controller_test.exs22
3 files changed, 41 insertions, 1 deletions
diff --git a/lib/pleroma/activity/search.ex b/lib/pleroma/activity/search.ex
index ceb365bb3..124b663b2 100644
--- a/lib/pleroma/activity/search.ex
+++ b/lib/pleroma/activity/search.ex
@@ -54,7 +54,13 @@ defmodule Pleroma.Activity.Search do
from([a, o] in q,
where:
fragment(
- "to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?)",
+ "(to_tsvector('english', ?->>'summary') @@ plainto_tsquery('english', ?))",
+ o.data,
+ ^search_query
+ ),
+ or_where:
+ fragment(
+ "(to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?))",
o.data,
^search_query
)
diff --git a/priv/repo/migrations/20200327212356_add_summary_to_fts_index.exs b/priv/repo/migrations/20200327212356_add_summary_to_fts_index.exs
new file mode 100644
index 000000000..4f0c70726
--- /dev/null
+++ b/priv/repo/migrations/20200327212356_add_summary_to_fts_index.exs
@@ -0,0 +1,12 @@
+defmodule Pleroma.Repo.Migrations.AddSummaryToFtsIndex do
+ use Ecto.Migration
+
+ def change do
+ create_if_not_exists(
+ index(:objects, ["(to_tsvector('english', data->>'summary'))"],
+ using: :gin,
+ name: :objects_summary_fts
+ )
+ )
+ end
+end
diff --git a/test/web/mastodon_api/controllers/search_controller_test.exs b/test/web/mastodon_api/controllers/search_controller_test.exs
index 11133ff66..d41f6b60a 100644
--- a/test/web/mastodon_api/controllers/search_controller_test.exs
+++ b/test/web/mastodon_api/controllers/search_controller_test.exs
@@ -37,6 +37,28 @@ defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
end
end
+ test "activity search", %{conn: conn} do
+ user = insert(:user)
+
+ {:ok, activity} = CommonAPI.post(user, %{"status" => "Query in the body 2hu"})
+
+ {:ok, activity_two} =
+ CommonAPI.post(user, %{"spoiler_text" => "2hu", "status" => "Query in the subject"})
+
+ {:ok, _activity} = CommonAPI.post(user, %{"status" => "No query"})
+
+ statuses =
+ conn
+ |> get("/api/v2/search", %{"q" => "2hu"})
+ |> json_response(200)
+ |> Map.get("statuses")
+ |> Enum.map(& &1["id"])
+
+ assert length(statuses) == 2
+ assert activity.id in statuses
+ assert activity_two.id in statuses
+ end
+
test "search", %{conn: conn} do
user = insert(:user)
user_two = insert(:user, %{nickname: "shp@shitposter.club"})