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-22 15:47:41 +00:00
|
|
|
{:ok, _} =
|
|
|
|
Pleroma.HTTP.post(
|
|
|
|
"#{endpoint}/indexes/objects/settings/searchable-attributes",
|
|
|
|
Jason.encode!([
|
|
|
|
"content"
|
|
|
|
])
|
|
|
|
)
|
|
|
|
|
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-22 19:53:18 +00:00
|
|
|
|> Stream.map(&Pleroma.Search.Meilisearch.object_to_search_data/1)
|
|
|
|
|> Stream.filter(fn o -> not is_nil(o) end)
|
2021-08-16 19:30:56 +00:00
|
|
|
|> Stream.chunk_every(chunk_size)
|
2021-08-22 16:38:03 +00:00
|
|
|
|> Stream.transform(0, fn objects, acc ->
|
|
|
|
new_acc = acc + Enum.count(objects)
|
|
|
|
|
|
|
|
IO.puts("Indexed #{new_acc} entries")
|
|
|
|
|
|
|
|
{[objects], new_acc}
|
2021-08-16 19:30:56 +00:00
|
|
|
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-22 16:38:03 +00:00
|
|
|
{:ok, _} =
|
|
|
|
Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [],
|
|
|
|
timeout: :infinity
|
|
|
|
)
|
2021-08-16 19:24:31 +00:00
|
|
|
end
|
2021-08-15 18:53:04 +00:00
|
|
|
end
|