Add activity summary to GIN full text search

This commit is contained in:
Sergey Suprunenko 2020-03-31 19:09:34 +02:00
parent 8bd5ad8664
commit 72ef8bc7b0
No known key found for this signature in database
GPG Key ID: 5DCA7D1BE3914F9C
3 changed files with 41 additions and 1 deletions

View File

@ -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
)

View File

@ -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

View File

@ -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"})