summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLain Soykaf <lain@soykaf.club>2021-01-22 11:43:31 +0100
committerLain Soykaf <lain@soykaf.club>2021-01-22 11:43:31 +0100
commite30528fe4255510b5edd6b0f39855942029e29f8 (patch)
tree4d4d76e634760d500d8d1cb7b8fe912379102ade
parentc2186a62d54043ea9638d33f80c7576aba9783e8 (diff)
Search: Move fts index to the activities.gin-speedup
-rw-r--r--lib/pleroma/activity/search.ex4
-rw-r--r--priv/repo/migrations/20210120161014_move_fts_index.exs54
2 files changed, 56 insertions, 2 deletions
diff --git a/lib/pleroma/activity/search.ex b/lib/pleroma/activity/search.ex
index 382c81118..4511b2533 100644
--- a/lib/pleroma/activity/search.ex
+++ b/lib/pleroma/activity/search.ex
@@ -57,8 +57,8 @@ defmodule Pleroma.Activity.Search do
from([a, o] in q,
where:
fragment(
- "to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?)",
- o.data,
+ "? @@ plainto_tsquery('english', ?)",
+ a.fts_content,
^search_query
)
)
diff --git a/priv/repo/migrations/20210120161014_move_fts_index.exs b/priv/repo/migrations/20210120161014_move_fts_index.exs
new file mode 100644
index 000000000..d32b56214
--- /dev/null
+++ b/priv/repo/migrations/20210120161014_move_fts_index.exs
@@ -0,0 +1,54 @@
+defmodule Pleroma.Repo.Migrations.MoveFtsIndex do
+ use Ecto.Migration
+
+ def up do
+ # Drop the old index
+ drop_if_exists(
+ index(:objects, ["(to_tsvector('english', data->>'content'))"],
+ using: :gin,
+ name: :objects_fts
+ )
+ )
+
+ alter table(:activities) do
+ add(:fts_content, :tsvector)
+ end
+
+ execute("CREATE FUNCTION activities_fts_update() RETURNS trigger AS $$
+ declare
+ content text := '';
+ begin
+ if new.data->>'type' = 'Create' then
+
+ select objects.data->>'content'
+ from objects
+ into content
+ where objects.data->>'id' = new.data->>'object';
+
+ new.fts_content := to_tsvector('english', content);
+
+ end if;
+
+ return new;
+ end
+ $$ LANGUAGE plpgsql")
+
+ create_if_not_exists(index(:activities, ["fts_content"], using: :gin, name: :activities_fts))
+ execute("CREATE TRIGGER ts_vector_activities_update BEFORE INSERT ON activities
+ FOR EACH ROW EXECUTE PROCEDURE activities_fts_update()
+ ")
+ end
+
+ def down do
+ execute("drop function if exists actitivies_fts_update()")
+ execute("drop trigger if existis ts_vector_activities_update")
+ drop_if_exists(index(:activities, ["fts_content"], using: :gin, name: :activities_fts))
+
+ create_if_not_exists(
+ index(:objects, ["(to_tsvector('english', data->>'content'))"],
+ using: :gin,
+ name: :objects_fts
+ )
+ )
+ end
+end