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
15 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
19 "#{endpoint}/indexes/objects/settings/ranking-rules",
33 "#{endpoint}/indexes/objects/settings/searchable-attributes",
41 Pleroma.Repo.transaction(
45 # Only index public posts which are notes and have some text
47 fragment("data->>'type' = 'Note'") and
48 fragment("LENGTH(data->>'content') > 0") and
49 fragment("data->'to' \\? ?", ^Pleroma.Constants.as_public()),
50 order_by: [desc: fragment("data->'published'")]
54 |> Stream.map(&Pleroma.Search.Meilisearch.object_to_search_data/1)
55 |> Stream.filter(fn o -> not is_nil(o) end)
56 |> Stream.chunk_every(chunk_size)
57 |> Stream.transform(0, fn objects, acc ->
58 new_acc = acc + Enum.count(objects)
60 # Reset to the beginning of the line and rewrite it
62 IO.write("Indexed #{new_acc} entries")
66 |> Stream.each(fn objects ->
69 "#{endpoint}/indexes/objects/documents",
70 Jason.encode!(objects)
73 if not Map.has_key?(Jason.decode!(result.body), "updateId") do
74 IO.puts("Failed to index: #{result}")
88 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
91 Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [],