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 22:37:43 +00:00
|
|
|
chunk_size = 10_000
|
2021-08-16 19:30:56 +00:00
|
|
|
|
|
|
|
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
|
2021-08-16 21:57:53 +00:00
|
|
|
fragment("LENGTH(data->>'content') > 0") and
|
2021-08-16 19:30:56 +00:00
|
|
|
fragment("data->'to' \\? ?", ^Pleroma.Constants.as_public()),
|
2021-08-16 21:30:14 +00:00
|
|
|
order_by: [desc: fragment("data->'published'")]
|
2021-08-16 19:30:56 +00:00
|
|
|
),
|
|
|
|
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"])
|
2021-08-16 21:57:53 +00:00
|
|
|
{:ok, content} = FastSanitize.strip_tags(data["content"])
|
2021-08-16 21:06:32 +00:00
|
|
|
|
|
|
|
%{
|
|
|
|
id: object.id,
|
2021-08-16 21:57:53 +00:00
|
|
|
content: content,
|
2021-08-16 21:06:32 +00:00
|
|
|
ap: data["id"],
|
|
|
|
published: published |> DateTime.to_unix()
|
|
|
|
}
|
2021-08-16 19:30:56 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|> Stream.each(fn objects ->
|
2021-08-16 21:30:14 +00:00
|
|
|
{:ok, result} =
|
2021-08-16 19:30:56 +00:00
|
|
|
Pleroma.HTTP.post(
|
|
|
|
"#{endpoint}/indexes/objects/documents",
|
|
|
|
Jason.encode!(objects)
|
|
|
|
)
|
2021-08-16 21:30:14 +00:00
|
|
|
|
|
|
|
if not Map.has_key?(Jason.decode!(result.body), "updateId") do
|
|
|
|
IO.puts("Failed to index: #{result}")
|
|
|
|
end
|
2021-08-16 19:30:56 +00:00
|
|
|
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])
|
|
|
|
|
2021-08-16 21:57:53 +00:00
|
|
|
{:ok, _} = Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects", "", [], [])
|
2021-08-16 19:24:31 +00:00
|
|
|
end
|
2021-08-15 18:53:04 +00:00
|
|
|
end
|