Support for setting PGroonga indexing options

This commit is contained in:
itepechi 2023-08-06 06:16:38 +09:00
parent 3859c4bbcf
commit 2634723958
2 changed files with 40 additions and 34 deletions

View File

@ -331,51 +331,59 @@ defmodule Mix.Tasks.Pleroma.Database do
shell_info("Error: #{inspect(msg, pretty: true)}") shell_info("Error: #{inspect(msg, pretty: true)}")
else else
rum_enabled = Pleroma.Config.get([:database, :rum_enabled]) rum_enabled = Pleroma.Config.get([:database, :rum_enabled])
pgroonga_enabled = Pleroma.Config.get([:database, :pgroonga_enabled]) shell_info("Recreate index, RUM: #{rum_enabled}")
shell_info("Recreate index, RUM: #{rum_enabled}, PGroonga: #{pgroonga_enabled}")
# Note SQL below needs to be kept up-to-date with latest GIN or RUM index definition in future # Note SQL below needs to be kept up-to-date with latest GIN or RUM index definition in future
cond do if rum_enabled do
rum_enabled -> Ecto.Adapters.SQL.query!(
Ecto.Adapters.SQL.query!( Pleroma.Repo,
Pleroma.Repo, "CREATE OR REPLACE FUNCTION objects_fts_update() RETURNS trigger AS $$ BEGIN
"CREATE OR REPLACE FUNCTION objects_fts_update() RETURNS trigger AS $$ BEGIN
new.fts_content := to_tsvector(new.data->>'content'); new.fts_content := to_tsvector(new.data->>'content');
RETURN new; RETURN new;
END END
$$ LANGUAGE plpgsql", $$ LANGUAGE plpgsql",
[], [],
timeout: :infinity timeout: :infinity
) )
shell_info("Refresh RUM index") shell_info("Refresh RUM index")
Ecto.Adapters.SQL.query!(Pleroma.Repo, "UPDATE objects SET updated_at = NOW();") 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;")
pgroonga_enabled -> Ecto.Adapters.SQL.query!(
Ecto.Adapters.SQL.query!(Pleroma.Repo, "DROP INDEX IF EXISTS object_content_pgroonga;") Pleroma.Repo,
"CREATE INDEX CONCURRENTLY objects_fts ON objects USING gin(to_tsvector('#{tsconfig}', data->>'content'));",
Ecto.Adapters.SQL.query!( [],
Pleroma.Repo, timeout: :infinity
"CREATE INDEX object_content_pgroonga ON objects USING pgroonga ((data->'content')) WITH (tokenizer='TokenMecab');", )
[],
timeout: :infinity
)
true ->
Ecto.Adapters.SQL.query!(Pleroma.Repo, "DROP INDEX IF EXISTS objects_fts;")
Ecto.Adapters.SQL.query!(
Pleroma.Repo,
"CREATE INDEX CONCURRENTLY objects_fts ON objects USING gin(to_tsvector('#{tsconfig}', data->>'content'));",
[],
timeout: :infinity
)
end end
shell_info('Done.') shell_info('Done.')
end end
end end
def run(["set_pgroonga_options", pg_options]) do
start_pleroma()
pgroonga_enabled = Pleroma.Config.get([:database, :pgroonga_enabled])
if pgroonga_enabled do
shell_info("Recreate index with options #{pg_options}")
Ecto.Adapters.SQL.query!(Pleroma.Repo, "DROP INDEX IF EXISTS object_content_pgroonga;")
Ecto.Adapters.SQL.query!(
Pleroma.Repo,
"CREATE INDEX object_content_pgroonga ON objects USING pgroonga ((data->'content')) WITH (#{pg_options});",
[],
timeout: :infinity
)
shell_info('Done.')
else
shell_info('PGroonga is not enabled.')
end
end
# Rolls back a specific migration (leaving subsequent migrations applied). # Rolls back a specific migration (leaving subsequent migrations applied).
# WARNING: imposes a risk of unrecoverable data loss — proceed at your own responsibility. # WARNING: imposes a risk of unrecoverable data loss — proceed at your own responsibility.
# Based on https://stackoverflow.com/a/53825840 # Based on https://stackoverflow.com/a/53825840

View File

@ -11,9 +11,7 @@ defmodule Pleroma.Repo.Migrations.CreatePgroongaIndex do
) )
) )
execute( execute("CREATE INDEX object_content_pgroonga ON objects USING pgroonga ((data->'content'))")
"CREATE INDEX object_content_pgroonga ON objects USING pgroonga ((data->'content')) WITH (tokenizer='TokenMecab')"
)
end end
def down do def down do