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 Pleroma.Repo.transaction(
37 # Only index public posts which are notes and have some text
39 fragment("data->>'type' = 'Note'") and
40 fragment("LENGTH(data->>'source') > 0") and
41 fragment("data->'to' \\? ?", ^Pleroma.Constants.as_public()),
42 order_by: [desc: fragment("data->'published'")]
46 |> Stream.chunk_every(chunk_size)
47 |> Stream.transform(0, fn objects, acc ->
48 new_acc = acc + Enum.count(objects)
50 IO.puts("Indexed #{new_acc} entries")
54 |> Stream.map(fn objects ->
55 Enum.map(objects, fn object ->
58 {:ok, published, _} = DateTime.from_iso8601(data["published"])
62 source: data["source"],
64 published: published |> DateTime.to_unix()
68 |> Stream.each(fn objects ->
71 "#{endpoint}/indexes/objects/documents",
72 Jason.encode!(objects)
75 if not Map.has_key?(Jason.decode!(result.body), "updateId") do
76 IO.puts("Failed to index: #{result}")
88 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
91 Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [], [])
93 if not Map.has_key?(Jason.decode!(result.body), "updateId") do
94 IO.puts("Failed to clear: #{result}")