2019-12-09 04:16:13 +01:00
|
|
|
defmodule Mix.Tasks.ReindexAll do
|
|
|
|
use Mix.Task
|
|
|
|
|
2024-05-25 20:03:45 +02:00
|
|
|
alias PhilomenaQuery.Search
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
alias Philomena.{
|
|
|
|
Comments.Comment,
|
|
|
|
Galleries.Gallery,
|
|
|
|
Posts.Post,
|
|
|
|
Images.Image,
|
|
|
|
Reports.Report,
|
2021-01-18 19:00:35 +01:00
|
|
|
Tags.Tag,
|
|
|
|
Filters.Filter
|
2020-01-11 05:20:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:00:35 +01:00
|
|
|
alias Philomena.{Comments, Galleries, Posts, Images, Tags, Filters}
|
2019-12-09 04:16:13 +01:00
|
|
|
alias Philomena.Polymorphic
|
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
2021-04-01 17:16:47 +02:00
|
|
|
@indices [
|
|
|
|
{Images, Image},
|
|
|
|
{Comments, Comment},
|
|
|
|
{Galleries, Gallery},
|
|
|
|
{Tags, Tag},
|
|
|
|
{Posts, Post},
|
|
|
|
{Filters, Filter}
|
|
|
|
]
|
|
|
|
|
2024-05-25 20:03:45 +02:00
|
|
|
@shortdoc "Destroys and recreates all OpenSearch indices."
|
2020-10-26 22:01:29 +01:00
|
|
|
@requirements ["app.start"]
|
2020-08-08 02:23:36 +02:00
|
|
|
@impl Mix.Task
|
|
|
|
def run(args) do
|
|
|
|
if Mix.env() == :prod and not Enum.member?(args, "--i-know-what-im-doing") do
|
|
|
|
raise "do not run this task unless you know what you're doing"
|
2019-12-09 04:16:13 +01:00
|
|
|
end
|
|
|
|
|
2021-04-01 17:16:47 +02:00
|
|
|
@indices
|
|
|
|
|> Enum.map(fn {context, schema} ->
|
|
|
|
Task.async(fn ->
|
2024-05-25 20:03:45 +02:00
|
|
|
Search.delete_index!(schema)
|
|
|
|
Search.create_index!(schema)
|
2021-04-01 17:16:47 +02:00
|
|
|
|
2024-05-25 20:03:45 +02:00
|
|
|
Search.reindex(preload(schema, ^context.indexing_preloads()), schema)
|
2021-04-01 17:16:47 +02:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|> Task.await_many(:infinity)
|
2019-12-09 04:16:13 +01:00
|
|
|
|
|
|
|
# Reports are a bit special
|
|
|
|
|
2024-05-25 20:03:45 +02:00
|
|
|
Search.delete_index!(Report)
|
|
|
|
Search.create_index!(Report)
|
2019-12-09 04:16:13 +01:00
|
|
|
|
|
|
|
Report
|
|
|
|
|> preload([:user, :admin])
|
|
|
|
|> Repo.all()
|
|
|
|
|> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type])
|
2024-05-25 20:03:45 +02:00
|
|
|
|> Enum.map(&Search.index_document(&1, Report))
|
2019-12-09 04:16:13 +01:00
|
|
|
end
|
2019-12-24 22:14:42 +01:00
|
|
|
end
|