2021-08-15 18:53:04 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Mix.Tasks.Pleroma.Search.Meilisearch do
|
2021-08-16 07:18:01 +00:00
|
|
|
require Logger
|
2021-08-15 18:53:04 +00:00
|
|
|
|
2021-08-16 07:18:01 +00:00
|
|
|
import Mix.Pleroma
|
2021-08-15 18:53:04 +00:00
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
def run(["index"]) do
|
|
|
|
start_pleroma()
|
|
|
|
|
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
2021-08-16 07:18:01 +00:00
|
|
|
{:ok, _} =
|
|
|
|
Pleroma.HTTP.post(
|
|
|
|
"#{endpoint}/indexes/objects/settings/ranking-rules",
|
|
|
|
Jason.encode!([
|
|
|
|
"desc(id)",
|
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"wordsPosition",
|
|
|
|
"exactness"
|
|
|
|
])
|
|
|
|
)
|
|
|
|
|
2021-08-15 18:53:04 +00:00
|
|
|
Pleroma.Repo.chunk_stream(
|
|
|
|
from(Pleroma.Object,
|
|
|
|
where: fragment("data->>'type' = 'Note'") and fragment("LENGTH(data->>'source') > 0")
|
|
|
|
),
|
2021-08-16 07:18:01 +00:00
|
|
|
200,
|
2021-08-15 18:53:04 +00:00
|
|
|
:batches
|
|
|
|
)
|
|
|
|
|> Stream.map(fn objects ->
|
|
|
|
Enum.map(objects, fn object ->
|
|
|
|
data = object.data
|
|
|
|
%{id: object.id, source: data["source"], ap: data["id"]}
|
|
|
|
end)
|
|
|
|
end)
|
2021-08-16 07:18:01 +00:00
|
|
|
|> Stream.each(fn objects ->
|
2021-08-15 18:53:04 +00:00
|
|
|
{:ok, _} =
|
|
|
|
Pleroma.HTTP.post(
|
|
|
|
"#{endpoint}/indexes/objects/documents",
|
2021-08-16 07:18:01 +00:00
|
|
|
Jason.encode!(objects)
|
2021-08-15 18:53:04 +00:00
|
|
|
)
|
2021-08-16 07:18:01 +00:00
|
|
|
|
|
|
|
IO.puts("Indexed #{Enum.count(objects)} entries")
|
2021-08-15 18:53:04 +00:00
|
|
|
end)
|
|
|
|
|> Stream.run()
|
|
|
|
end
|
|
|
|
end
|