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'")]
52 |> Stream.map(&Pleroma.Search.Meilisearch.object_to_search_data/1)
53 |> Stream.filter(fn o -> not is_nil(o) end)
54 |> Stream.chunk_every(chunk_size)
55 |> Stream.transform(0, fn objects, acc ->
56 new_acc = acc + Enum.count(objects)
58 # Reset to the beginning of the line and rewrite it
60 IO.write("Indexed #{new_acc} entries")
64 |> Stream.each(fn objects ->
67 "/indexes/objects/documents",
71 if not Map.has_key?(result, "updateId") do
72 IO.puts("Failed to index: #{inspect(result)}")
86 meili_delete!("/indexes/objects/documents")
89 def run(["show-private-key", master_key]) do
92 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
96 Path.join(endpoint, "/keys"),
97 [{"X-Meili-API-Key", master_key}]
100 decoded = Jason.decode!(result.body)
102 if decoded["private"] do
103 IO.puts(decoded["private"])
105 IO.puts("Error fetching the key, check the master key is correct: #{inspect(decoded)}")