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!([
|
|
|
|
"desc(id)",
|
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"wordsPosition",
|
|
|
|
"exactness"
|
|
|
|
])
|
|
|
|
)
|
|
|
|
|
2021-08-15 18:53:04 +00:00
|
|
|
Pleroma.Repo.chunk_stream(
|
|
|
|
from(Pleroma.Object,
|
2021-08-16 19:24:31 +00:00
|
|
|
# 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())
|
2021-08-15 18:53:04 +00:00
|
|
|
),
|
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
|
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
|