Implement search with PostgreSQL trigrams

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Rename function to reflect that we only get one result

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Add loggers and make Ecto call parallels during search

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Implement trigrams for events & replace pg similarity operator % with <%

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Fix tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-02-21 18:11:49 +01:00
parent 131152abac
commit 4ec40d601b
18 changed files with 422 additions and 141 deletions

View File

@@ -2,6 +2,15 @@ defmodule Mobilizon.Repo.Migrations.Prerequites do
use Ecto.Migration
def up do
IO.puts("\n
#########################################################
# If the CREATE EXTENSION or DROP EXTENSION calls fail, #
# please manually execute them with an authorized #
# PostgreSQL user with SUPER USER role. #
#########################################################
\n
")
execute("""
CREATE TYPE datetimetz AS (
dt timestamptz,
@@ -10,10 +19,23 @@ defmodule Mobilizon.Repo.Migrations.Prerequites do
""")
execute("CREATE EXTENSION IF NOT EXISTS postgis")
execute("CREATE EXTENSION IF NOT EXISTS pg_trgm")
execute("CREATE EXTENSION IF NOT EXISTS unaccent")
end
def down do
IO.puts("\n
#########################################################
# If the CREATE EXTENSION or DROP EXTENSION calls fail, #
# please manually execute them with an authorized #
# PostgreSQL user with SUPER USER role. #
#########################################################
\n
")
execute("DROP TYPE IF EXISTS datetimetz;")
execute("DROP EXTENSION IF EXISTS postgis")
execute("DROP EXTENSION IF EXISTS pg_trgm")
execute("DROP EXTENSION IF EXISTS unaccent")
end
end

View File

@@ -0,0 +1,48 @@
defmodule Mobilizon.Repo.Migrations.CreateSearchIndexes do
use Ecto.Migration
def change do
IO.puts("\n
#########################################################
# If the CREATE EXTENSION or DROP EXTENSION calls fail, #
# please manually execute them with an authorized #
# PostgreSQL user with SUPER USER role. #
#########################################################
\n
")
try do
execute("CREATE EXTENSION IF NOT EXISTS pg_trgm", "DROP EXTENSION IF EXISTS pg_trgm")
execute("CREATE EXTENSION IF NOT EXISTS unaccent", "DROP EXTENSION IF EXISTS unaccent")
execute(
"CREATE OR REPLACE FUNCTION public.f_unaccent(text)
RETURNS text AS
$func$
SELECT public.unaccent('public.unaccent', $1)
$func$ LANGUAGE sql IMMUTABLE;",
"DROP FUNCTION IF EXISTS public.f_unaccent"
)
execute(
"CREATE INDEX \"event_title_trigram\" ON \"events\" USING GIN (f_unaccent(title) gin_trgm_ops)",
"DROP INDEX IF EXISTS event_title_trigram"
)
execute(
"CREATE INDEX \"actor_preferred_username_trigram\" ON \"actors\"
USING GIN (f_unaccent(preferred_username) gin_trgm_ops)",
"DROP INDEX IF EXISTS actor_preferred_username_trigram"
)
execute(
"CREATE INDEX \"actor_name_trigram\" ON \"actors\"
USING GIN (f_unaccent(name) gin_trgm_ops)",
"DROP INDEX IF EXISTS actor_name_trigram"
)
rescue
e in Postgrex.Error ->
IO.puts(e.message)
end
end
end