From 72ef8bc7b08540aed95466e3fce36a682fb00c7a Mon Sep 17 00:00:00 2001 From: Sergey Suprunenko Date: Tue, 31 Mar 2020 19:09:34 +0200 Subject: [PATCH] Add activity summary to GIN full text search --- lib/pleroma/activity/search.ex | 8 +++++++- .../20200327212356_add_summary_to_fts_index.exs | 12 ++++++++++++ .../controllers/search_controller_test.exs | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20200327212356_add_summary_to_fts_index.exs 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"})