From 6650c69aafd786314204c6617d979393377a7bb7 Mon Sep 17 00:00:00 2001 From: Sergey Suprunenko Date: Tue, 31 Mar 2020 19:10:10 +0200 Subject: Add activity summary to RUM full text search --- lib/pleroma/activity/search.ex | 6 +++ ...20200327223848_add_summary_to_fts_index_two.exs | 48 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 priv/repo/optional_migrations/rum_indexing/20200327223848_add_summary_to_fts_index_two.exs diff --git a/lib/pleroma/activity/search.ex b/lib/pleroma/activity/search.ex index 124b663b2..aea371102 100644 --- a/lib/pleroma/activity/search.ex +++ b/lib/pleroma/activity/search.ex @@ -75,6 +75,12 @@ defmodule Pleroma.Activity.Search do o.fts_content, ^search_query ), + or_where: + fragment( + "? @@ plainto_tsquery('english', ?)", + o.fts_summary, + ^search_query + ), order_by: [fragment("? <=> now()::date", o.inserted_at)] ) end diff --git a/priv/repo/optional_migrations/rum_indexing/20200327223848_add_summary_to_fts_index_two.exs b/priv/repo/optional_migrations/rum_indexing/20200327223848_add_summary_to_fts_index_two.exs new file mode 100644 index 000000000..2dc3c5d09 --- /dev/null +++ b/priv/repo/optional_migrations/rum_indexing/20200327223848_add_summary_to_fts_index_two.exs @@ -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 -- cgit v1.2.3