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.chunk_every(chunk_size)
55 |> Stream.map(fn objects ->
56 Enum.map(objects, fn object ->
60 case data["content"] do
61 [nil | rest] -> to_string(rest)
65 {:ok, published, _} = DateTime.from_iso8601(data["published"])
68 with {:ok, scrubbed} <- FastSanitize.strip_tags(content_str),
69 trimmed <- String.trim(scrubbed) do
73 # Only index if there is anything in the string. If there is a single symbol,
74 # it's probably a dot from mastodon posts with just the picture
75 if String.length(content) > 1 do
80 published: published |> DateTime.to_unix()
86 |> Enum.filter(fn o -> not is_nil(o) end)
88 |> Stream.transform(0, fn objects, acc ->
89 new_acc = acc + Enum.count(objects)
91 IO.puts("Indexed #{new_acc} entries")
95 |> Stream.each(fn objects ->
98 "#{endpoint}/indexes/objects/documents",
99 Jason.encode!(objects)
102 if not Map.has_key?(Jason.decode!(result.body), "updateId") do
103 IO.puts("Failed to index: #{result}")
112 def run(["clear"]) do
115 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
118 Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [],