2020-01-23 00:55:07 +01:00
|
|
|
defmodule Mobilizon.Service.Workers.BuildSearch do
|
2019-10-22 10:25:28 +02:00
|
|
|
@moduledoc """
|
2019-11-04 15:10:58 +01:00
|
|
|
Worker to build search results
|
2019-10-22 10:25:28 +02:00
|
|
|
"""
|
|
|
|
|
2020-01-28 20:15:59 +01:00
|
|
|
alias Ecto.Adapters.SQL
|
|
|
|
|
2019-11-04 15:10:58 +01:00
|
|
|
alias Mobilizon.Events
|
2019-10-22 10:25:28 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2020-06-24 16:33:59 +02:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2019-10-22 10:25:28 +02:00
|
|
|
alias Mobilizon.Storage.Repo
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
use Mobilizon.Service.Workers.Helper, queue: "search"
|
2019-11-04 15:10:58 +01:00
|
|
|
|
|
|
|
@impl Oban.Worker
|
2020-08-12 14:34:19 +02:00
|
|
|
def perform(%Job{
|
|
|
|
args: %{"op" => "insert_search_event", "event_id" => event_id}
|
|
|
|
}) do
|
2019-11-04 15:10:58 +01:00
|
|
|
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id) do
|
|
|
|
insert_search_event(event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
def perform(%Job{
|
|
|
|
args: %{"op" => "update_search_event", "event_id" => event_id}
|
|
|
|
}) do
|
2019-11-04 15:10:58 +01:00
|
|
|
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id) do
|
2019-11-15 18:36:47 +01:00
|
|
|
insert_search_event(event)
|
2019-11-04 15:10:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-22 10:25:28 +02:00
|
|
|
def insert_search_event(%Event{} = event) do
|
|
|
|
SQL.query(
|
|
|
|
Repo,
|
|
|
|
"""
|
|
|
|
INSERT INTO event_search(id, title, document) VALUES ($1, $2, (
|
|
|
|
SELECT
|
|
|
|
setweight(to_tsvector(unaccent($2)), 'A') ||
|
|
|
|
setweight(to_tsvector(unaccent(coalesce($4, ' '))), 'B') ||
|
|
|
|
setweight(to_tsvector(unaccent($3)), 'C')
|
|
|
|
)
|
2019-11-15 18:36:47 +01:00
|
|
|
) ON CONFLICT (id) DO UPDATE SET title = $2, document = (
|
|
|
|
SELECT
|
|
|
|
setweight(to_tsvector(unaccent($2)), 'A') ||
|
|
|
|
setweight(to_tsvector(unaccent(coalesce($4, ' '))), 'B') ||
|
|
|
|
setweight(to_tsvector(unaccent($3)), 'C')
|
|
|
|
);
|
2019-10-22 10:25:28 +02:00
|
|
|
""",
|
|
|
|
[
|
|
|
|
event.id,
|
|
|
|
event.title,
|
2020-07-31 11:19:42 +02:00
|
|
|
HTML.strip_tags_and_insert_spaces(event.description),
|
2019-10-22 10:25:28 +02:00
|
|
|
get_tags_string(event)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_tags_string(%Event{tags: tags}) do
|
2021-11-26 14:30:46 +01:00
|
|
|
Enum.map_join(tags, " ", & &1.title)
|
2019-10-22 10:25:28 +02:00
|
|
|
end
|
|
|
|
end
|