2019-11-04 15:10:58 +01:00
|
|
|
defmodule Mobilizon.Service.Workers.BuildSearchWorker 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
|
|
|
"""
|
|
|
|
|
2019-11-04 15:10:58 +01:00
|
|
|
alias Mobilizon.Events
|
2019-10-22 10:25:28 +02:00
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Storage.Repo
|
|
|
|
alias Ecto.Adapters.SQL
|
|
|
|
|
2019-11-04 15:10:58 +01:00
|
|
|
use Mobilizon.Service.Workers.WorkerHelper, queue: "search"
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
|
|
|
def perform(%{"op" => "insert_search_event", "event_id" => event_id}, _job) do
|
|
|
|
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id) do
|
|
|
|
insert_search_event(event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(%{"op" => "update_search_event", "event_id" => event_id}, _job) do
|
|
|
|
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id) do
|
|
|
|
update_search_event(event)
|
|
|
|
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')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
""",
|
|
|
|
[
|
|
|
|
event.id,
|
|
|
|
event.title,
|
|
|
|
HtmlSanitizeEx.strip_tags(event.description),
|
|
|
|
get_tags_string(event)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_search_event(%Event{} = event) do
|
|
|
|
SQL.query(
|
|
|
|
Repo,
|
|
|
|
"""
|
|
|
|
UPDATE event_search
|
|
|
|
SET document =
|
|
|
|
(SELECT
|
|
|
|
setweight(to_tsvector(unaccent($2)), 'A') ||
|
|
|
|
setweight(to_tsvector(unaccent(coalesce($4, ' '))), 'B') ||
|
|
|
|
setweight(to_tsvector(unaccent($3)), 'C')
|
|
|
|
),
|
|
|
|
title = $2
|
|
|
|
WHERE id = $1;
|
|
|
|
""",
|
|
|
|
[
|
|
|
|
event.id,
|
|
|
|
event.title,
|
|
|
|
HtmlSanitizeEx.strip_tags(event.description),
|
|
|
|
get_tags_string(event)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_tags_string(%Event{tags: tags}) do
|
|
|
|
tags
|
|
|
|
|> Enum.map(& &1.title)
|
|
|
|
|> Enum.join(" ")
|
|
|
|
end
|
|
|
|
end
|