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-16 19:24:31 +00:00
|
|
|
require Pleroma.Constants
|
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!([
|
2021-08-16 21:06:32 +00:00
|
|
|
"desc(published)",
|
2021-08-16 07:18:01 +00:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"wordsPosition",
|
|
|
|
"exactness"
|
|
|
|
])
|
|
|
|
)
|
|
|
|
|
2021-08-16 19:30:56 +00:00
|
|
|
chunk_size = 100_000
|
|
|
|
|
|
|
|
Pleroma.Repo.transaction(
|
|
|
|
fn ->
|
|
|
|
Pleroma.Repo.stream(
|
|
|
|
from(Pleroma.Object,
|
|
|
|
# Only index public posts which are notes and have some text
|
|
|
|
where:
|
|
|
|
fragment("data->>'type' = 'Note'") and
|
|
|
|
fragment("LENGTH(data->>'source') > 0") and
|
|
|
|
fragment("data->'to' \\? ?", ^Pleroma.Constants.as_public()),
|
|
|
|
order_by: fragment("data->'published' DESC")
|
|
|
|
),
|
|
|
|
timeout: :infinity
|
2021-08-15 18:53:04 +00:00
|
|
|
)
|
2021-08-16 19:30:56 +00:00
|
|
|
|> Stream.chunk_every(chunk_size)
|
|
|
|
|> Stream.transform(0, fn objects, acc ->
|
|
|
|
new_acc = acc + Enum.count(objects)
|
2021-08-16 07:18:01 +00:00
|
|
|
|
2021-08-16 19:30:56 +00:00
|
|
|
IO.puts("Indexed #{new_acc} entries")
|
|
|
|
|
|
|
|
{[objects], new_acc}
|
|
|
|
end)
|
|
|
|
|> Stream.map(fn objects ->
|
|
|
|
Enum.map(objects, fn object ->
|
|
|
|
data = object.data
|
2021-08-16 21:06:32 +00:00
|
|
|
|
|
|
|
{:ok, published, _} = DateTime.from_iso8601(data["published"])
|
|
|
|
|
|
|
|
%{
|
|
|
|
id: object.id,
|
|
|
|
source: data["source"],
|
|
|
|
ap: data["id"],
|
|
|
|
published: published |> DateTime.to_unix()
|
|
|
|
}
|
2021-08-16 19:30:56 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|> Stream.each(fn objects ->
|
|
|
|
{:ok, _} =
|
|
|
|
Pleroma.HTTP.post(
|
|
|
|
"#{endpoint}/indexes/objects/documents",
|
|
|
|
Jason.encode!(objects)
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|> Stream.run()
|
|
|
|
end,
|
|
|
|
timeout: :infinity
|
|
|
|
)
|
2021-08-15 18:53:04 +00:00
|
|
|
end
|
2021-08-16 19:24:31 +00:00
|
|
|
|
|
|
|
def run(["clear"]) do
|
|
|
|
start_pleroma()
|
|
|
|
|
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
|
|
|
{:ok, _} = Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [], [])
|
|
|
|
end
|
2021-08-15 18:53:04 +00:00
|
|
|
end
|