Compare commits

...

3 Commits

Author SHA1 Message Date
Sergey Suprunenko
e7048471e0
Write both summary and content searches in the same where to keep other conditions 2020-03-31 20:49:54 +02:00
Sergey Suprunenko
6650c69aaf
Add activity summary to RUM full text search 2020-03-31 19:10:10 +02:00
Sergey Suprunenko
72ef8bc7b0
Add activity summary to GIN full text search 2020-03-31 19:09:34 +02:00
4 changed files with 96 additions and 4 deletions

View File

@ -54,6 +54,11 @@ defmodule Pleroma.Activity.Search do
from([a, o] in q,
where:
fragment(
"to_tsvector('english', ?->>'summary') @@ plainto_tsquery('english', ?)",
o.data,
^search_query
) or
fragment(
"to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?)",
o.data,
^search_query
@ -66,6 +71,11 @@ defmodule Pleroma.Activity.Search do
where:
fragment(
"? @@ plainto_tsquery('english', ?)",
o.fts_summary,
^search_query
) or
fragment(
"? @@ plainto_tsquery('english', ?)",
o.fts_content,
^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

@ -0,0 +1,48 @@
defmodule Pleroma.Repo.Migrations.AddSummaryToFtsIndexTwo do
use Ecto.Migration
def up do
drop_if_exists(
index(:objects, ["(to_tsvector('english', data->>'summary'))"],
using: :gin,
name: :objects_summary_fts
)
)
alter table(:objects) do
add(:fts_summary, :tsvector)
end
execute("""
CREATE OR REPLACE FUNCTION objects_fts_update() RETURNS trigger AS $$
begin
new.fts_summary := to_tsvector('english', new.data->>'summary');
new.fts_content := to_tsvector('english', new.data->>'content');
return new;
end
$$ LANGUAGE plpgsql
""")
end
def down do
alter table(:objects) do
remove(:fts_summary, :tsvector)
end
create_if_not_exists(
index(:objects, ["(to_tsvector('english', data->>'summary'))"],
using: :gin,
name: :objects_summary_fts
)
)
execute("""
CREATE OR REPLACE FUNCTION objects_fts_update() RETURNS trigger AS $$
begin
new.fts_content := to_tsvector('english', new.data->>'content');
return new;
end
$$ LANGUAGE plpgsql
""")
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"})