2a64385289922cf25645135f5be6b433054d761a
[akkoma] / lib / mix / tasks / pleroma / search / meilisearch.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Search.Meilisearch do
6 require Logger
7 require Pleroma.Constants
8
9 import Mix.Pleroma
10 import Ecto.Query
11
12 def run(["index"]) do
13 start_pleroma()
14
15 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
16
17 {:ok, _} =
18 Pleroma.HTTP.post(
19 "#{endpoint}/indexes/objects/settings/ranking-rules",
20 Jason.encode!([
21 "desc(id)",
22 "typo",
23 "words",
24 "proximity",
25 "attribute",
26 "wordsPosition",
27 "exactness"
28 ])
29 )
30
31 chunk_size = 100_000
32
33 Pleroma.Repo.transaction(
34 fn ->
35 Pleroma.Repo.stream(
36 from(Pleroma.Object,
37 # Only index public posts which are notes and have some text
38 where:
39 fragment("data->>'type' = 'Note'") and
40 fragment("LENGTH(data->>'source') > 0") and
41 fragment("data->'to' \\? ?", ^Pleroma.Constants.as_public()),
42 order_by: fragment("data->'published' DESC")
43 ),
44 timeout: :infinity
45 )
46 |> Stream.chunk_every(chunk_size)
47 |> Stream.transform(0, fn objects, acc ->
48 new_acc = acc + Enum.count(objects)
49
50 IO.puts("Indexed #{new_acc} entries")
51
52 {[objects], new_acc}
53 end)
54 |> Stream.map(fn objects ->
55 Enum.map(objects, fn object ->
56 data = object.data
57 %{id: object.id, source: data["source"], ap: data["id"]}
58 end)
59 end)
60 |> Stream.each(fn objects ->
61 {:ok, _} =
62 Pleroma.HTTP.post(
63 "#{endpoint}/indexes/objects/documents",
64 Jason.encode!(objects)
65 )
66 end)
67 |> Stream.run()
68 end,
69 timeout: :infinity
70 )
71 end
72
73 def run(["clear"]) do
74 start_pleroma()
75
76 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
77
78 {:ok, _} = Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [], [])
79 end
80 end