From a9984c6da78f27d56c67e7fcec12dbad0a1ebc67 Mon Sep 17 00:00:00 2001 From: Tusooa Zhu Date: Fri, 27 Aug 2021 21:43:50 -0400 Subject: [PATCH 1/2] Make activity search properly use GIN indexes The original approach to search in GIN indexes is to use `to_tsvector(text)` in the WHERE clause of the query. According to postgres docs [pdoc], this method does not make use of the index, while `to_tsvector(config, text)` does. This commit changed the query to use the two-argument `to_tsvector()`. [pdoc]: https://www.postgresql.org/docs/12/textsearch-tables.html To obtain the search config in use, we make a query to the db first. The `::regconfig::oid` hack is needed because Postgrex does not support regconfig type directly [postgrexbug]. I use the conversion from and to `oid` instead of `text` because I tested in the actual DB and querying using the conversion via `text` is slow just as the one-argument `to_tsvector()` variant. [postgrexbug]: https://github.com/elixir-ecto/postgrex/issues/502 BUG: https://git.pleroma.social/pleroma/pleroma/-/issues/2758 --- lib/pleroma/activity/search.ex | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/activity/search.ex b/lib/pleroma/activity/search.ex index a5923519c..09671f621 100644 --- a/lib/pleroma/activity/search.ex +++ b/lib/pleroma/activity/search.ex @@ -65,10 +65,17 @@ defmodule Pleroma.Activity.Search do end defp query_with(q, :gin, search_query, :plain) do + %{rows: [[tsc]]} = + Ecto.Adapters.SQL.query!( + Pleroma.Repo, + "select current_setting('default_text_search_config')::regconfig::oid;" + ) + from([a, o] in q, where: fragment( - "to_tsvector(?->>'content') @@ plainto_tsquery(?)", + "to_tsvector(?::oid::regconfig, ?->>'content') @@ plainto_tsquery(?)", + ^tsc, o.data, ^search_query ) @@ -76,10 +83,17 @@ defmodule Pleroma.Activity.Search do end defp query_with(q, :gin, search_query, :websearch) do + %{rows: [[tsc]]} = + Ecto.Adapters.SQL.query!( + Pleroma.Repo, + "select current_setting('default_text_search_config')::regconfig::oid;" + ) + from([a, o] in q, where: fragment( - "to_tsvector(?->>'content') @@ websearch_to_tsquery(?)", + "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)", + ^tsc, o.data, ^search_query ) From a80cb58ac1902fc3d86f82a3d2b0473140e36f0c Mon Sep 17 00:00:00 2001 From: Tusooa Zhu Date: Fri, 27 Aug 2021 22:31:22 -0400 Subject: [PATCH 2/2] Add changelog for !3519 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 231cac990..ae6b7506b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Unreleased-patch - Mastodon API: Activity Search fallbacks on status fetching after a DB Timeout/Error +- Make activity search properly use GIN indexes ## 2.4.0 - 2021-08-xx