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, meili_get!: 1]
14 def run(["index" | args]) do
17 is_reindex = "--reindex" in args
20 "/indexes/objects/settings/ranking-rules",
33 "/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'")]
53 count = query |> Pleroma.Repo.aggregate(:count, :data)
54 IO.puts("Entries to index: #{count}")
60 |> Stream.map(&Pleroma.Search.Meilisearch.object_to_search_data/1)
61 |> Stream.filter(fn o -> not is_nil(o) end)
62 |> Stream.chunk_every(chunk_size)
63 |> Stream.transform(0, fn objects, acc ->
64 new_acc = acc + Enum.count(objects)
66 # Reset to the beginning of the line and rewrite it
68 IO.write("Indexed #{new_acc} entries")
72 |> Stream.each(fn objects ->
75 |> Enum.filter(fn o ->
77 result = meili_get!("/indexes/objects/documents/#{o.id}")
79 # With >= 0.24.0 the name for "errorCode" is just "code"
81 if meili_get!("/version")["pkgVersion"] |> Version.match?(">= 0.24.0"),
85 # Filter out the already indexed documents.
86 # This is true when the document does not exist
87 result[error_code_key] == "document_not_found"
95 "/indexes/objects/documents",
99 if not Map.has_key?(result, "updateId") do
100 IO.puts("Failed to index: #{inspect(result)}")
111 def run(["clear"]) do
114 meili_delete!("/indexes/objects/documents")
117 def run(["show-private-key", master_key]) do
120 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
124 Path.join(endpoint, "/keys"),
125 [{"X-Meili-API-Key", master_key}]
128 decoded = Jason.decode!(result.body)
130 if decoded["private"] do
131 IO.puts(decoded["private"])
133 IO.puts("Error fetching the key, check the master key is correct: #{inspect(decoded)}")
137 def run(["stats"]) do
140 result = meili_get!("/indexes/objects/stats")
141 IO.puts("Number of entries: #{result["numberOfDocuments"]}")
142 IO.puts("Indexing? #{result["isIndexing"]}")