philomena/lib/mix/tasks/reindex_all.ex

54 lines
1.3 KiB
Elixir
Raw Normal View History

2019-12-09 04:16:13 +01:00
defmodule Mix.Tasks.ReindexAll do
use Mix.Task
alias Philomena.Elasticsearch
2020-01-11 05:20:19 +01:00
alias Philomena.{
Comments.Comment,
Galleries.Gallery,
Posts.Post,
Images.Image,
Reports.Report,
Tags.Tag
}
2019-12-09 04:16:13 +01:00
alias Philomena.{Comments, Galleries, Posts, Images, Tags}
alias Philomena.Polymorphic
alias Philomena.Repo
import Ecto.Query
@shortdoc "Destroys and recreates all Elasticsearch indices."
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
{:ok, _apps} = Application.ensure_all_started(:philomena)
2020-01-11 05:20:19 +01:00
for {context, schema} <- [
{Images, Image},
{Comments, Comment},
{Galleries, Gallery},
{Tags, Tag},
{Posts, Post}
] do
Elasticsearch.delete_index!(schema)
Elasticsearch.create_index!(schema)
2019-12-09 04:16:13 +01:00
Elasticsearch.reindex(preload(schema, ^context.indexing_preloads()), schema)
2019-12-09 04:16:13 +01:00
end
# Reports are a bit special
Elasticsearch.delete_index!(Report)
Elasticsearch.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])
|> Enum.map(&Elasticsearch.index_document(&1, Report))
2019-12-09 04:16:13 +01:00
end
end