1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Search.Meilisearch do
7 require Pleroma.Constants
12 import Pleroma.Search.Meilisearch, only: [meili_post!: 2, meili_delete!: 1]
18 "/indexes/objects/settings/ranking-rules",
31 "/indexes/objects/settings/searchable-attributes",
39 Pleroma.Repo.transaction(
43 # Only index public posts which are notes and have some text
45 fragment("data->>'type' = 'Note'") and
46 fragment("LENGTH(data->>'content') > 0") and
47 fragment("data->'to' \\? ?", ^Pleroma.Constants.as_public()),
48 order_by: [desc: fragment("data->'published'")]
51 count = query |> Pleroma.Repo.aggregate(:count, :data)
52 IO.puts("Entries to index: #{count}")
58 |> Stream.map(&Pleroma.Search.Meilisearch.object_to_search_data/1)
59 |> Stream.filter(fn o -> not is_nil(o) end)
60 |> Stream.chunk_every(chunk_size)
61 |> Stream.transform(0, fn objects, acc ->
62 new_acc = acc + Enum.count(objects)
64 # Reset to the beginning of the line and rewrite it
66 IO.write("Indexed #{new_acc} entries")
70 |> Stream.each(fn objects ->
73 "/indexes/objects/documents",
77 if not Map.has_key?(result, "updateId") do
78 IO.puts("Failed to index: #{inspect(result)}")
92 meili_delete!("/indexes/objects/documents")
95 def run(["show-private-key", master_key]) do
98 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
102 Path.join(endpoint, "/keys"),
103 [{"X-Meili-API-Key", master_key}]
106 decoded = Jason.decode!(result.body)
108 if decoded["private"] do
109 IO.puts(decoded["private"])
111 IO.puts("Error fetching the key, check the master key is correct: #{inspect(decoded)}")