summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2021-02-06 09:42:20 +0000
committerrinpatch <rinpatch@sdf.org>2021-02-06 09:42:20 +0000
commit6e68058b634dced932a1283f8470598b5cabdfeb (patch)
treecff4438fa7728536571f9c5ad91eef263c18cd5e /lib
parent4dd28b4bd1c49d61018ae607a6775649e4d323bf (diff)
parent8d4e0342e1b5ebbe486dc538e3c8fe81d53220e6 (diff)
Merge branch 'feat/allow_alt_text_search_config' into 'develop'
allow user defined text search config in database See merge request pleroma/pleroma!3275
Diffstat (limited to 'lib')
-rw-r--r--lib/mix/tasks/pleroma/database.ex47
-rw-r--r--lib/pleroma/activity/search.ex8
2 files changed, 51 insertions, 4 deletions
diff --git a/lib/mix/tasks/pleroma/database.ex b/lib/mix/tasks/pleroma/database.ex
index 6261910f0..2403ed581 100644
--- a/lib/mix/tasks/pleroma/database.ex
+++ b/lib/mix/tasks/pleroma/database.ex
@@ -167,4 +167,51 @@ defmodule Mix.Tasks.Pleroma.Database do
end)
|> Stream.run()
end
+
+ def run(["set_text_search_config", tsconfig]) do
+ start_pleroma()
+ %{rows: [[tsc]]} = Ecto.Adapters.SQL.query!(Pleroma.Repo, "SHOW default_text_search_config;")
+ shell_info("Current default_text_search_config: #{tsc}")
+
+ %{rows: [[db]]} = Ecto.Adapters.SQL.query!(Pleroma.Repo, "SELECT current_database();")
+ shell_info("Update default_text_search_config: #{tsconfig}")
+
+ %{messages: msg} =
+ Ecto.Adapters.SQL.query!(
+ Pleroma.Repo,
+ "ALTER DATABASE #{db} SET default_text_search_config = '#{tsconfig}';"
+ )
+
+ # non-exist config will not raise excpetion but only give >0 messages
+ if length(msg) > 0 do
+ shell_info("Error: #{inspect(msg, pretty: true)}")
+ else
+ rum_enabled = Pleroma.Config.get([:database, :rum_enabled])
+ shell_info("Recreate index, RUM: #{rum_enabled}")
+
+ # Note SQL below needs to be kept up-to-date with latest GIN or RUM index definition in future
+ if rum_enabled do
+ Ecto.Adapters.SQL.query!(
+ Pleroma.Repo,
+ "CREATE OR REPLACE FUNCTION objects_fts_update() RETURNS trigger AS $$ BEGIN
+ new.fts_content := to_tsvector(new.data->>'content');
+ RETURN new;
+ END
+ $$ LANGUAGE plpgsql"
+ )
+
+ shell_info("Refresh RUM index")
+ Ecto.Adapters.SQL.query!(Pleroma.Repo, "UPDATE objects SET updated_at = NOW();")
+ else
+ Ecto.Adapters.SQL.query!(Pleroma.Repo, "DROP INDEX IF EXISTS objects_fts;")
+
+ Ecto.Adapters.SQL.query!(
+ Pleroma.Repo,
+ "CREATE INDEX objects_fts ON objects USING gin(to_tsvector('#{tsconfig}', data->>'content')); "
+ )
+ end
+
+ shell_info('Done.')
+ end
+ end
end
diff --git a/lib/pleroma/activity/search.ex b/lib/pleroma/activity/search.ex
index 52e7c048d..ed898ba4f 100644
--- a/lib/pleroma/activity/search.ex
+++ b/lib/pleroma/activity/search.ex
@@ -64,7 +64,7 @@ defmodule Pleroma.Activity.Search do
from([a, o] in q,
where:
fragment(
- "to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?)",
+ "to_tsvector(?->>'content') @@ plainto_tsquery(?)",
o.data,
^search_query
)
@@ -75,7 +75,7 @@ defmodule Pleroma.Activity.Search do
from([a, o] in q,
where:
fragment(
- "to_tsvector('english', ?->>'content') @@ websearch_to_tsquery('english', ?)",
+ "to_tsvector(?->>'content') @@ websearch_to_tsquery(?)",
o.data,
^search_query
)
@@ -86,7 +86,7 @@ defmodule Pleroma.Activity.Search do
from([a, o] in q,
where:
fragment(
- "? @@ plainto_tsquery('english', ?)",
+ "? @@ plainto_tsquery(?)",
o.fts_content,
^search_query
),
@@ -98,7 +98,7 @@ defmodule Pleroma.Activity.Search do
from([a, o] in q,
where:
fragment(
- "? @@ websearch_to_tsquery('english', ?)",
+ "? @@ websearch_to_tsquery(?)",
o.fts_content,
^search_query
),