0b86fdececf550a1f83b0ea66f132e3b1009ac3b
[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 Pleroma.Repo.chunk_stream(
32 from(Pleroma.Object,
33 # Only index public posts which are notes and have some text
34 where:
35 fragment("data->>'type' = 'Note'") and
36 fragment("LENGTH(data->>'source') > 0") and
37 fragment("data->'to' \\? ?", ^Pleroma.Constants.as_public())
38 ),
39 200,
40 :batches
41 )
42 |> Stream.map(fn objects ->
43 Enum.map(objects, fn object ->
44 data = object.data
45 %{id: object.id, source: data["source"], ap: data["id"]}
46 end)
47 end)
48 |> Stream.each(fn objects ->
49 {:ok, _} =
50 Pleroma.HTTP.post(
51 "#{endpoint}/indexes/objects/documents",
52 Jason.encode!(objects)
53 )
54
55 IO.puts("Indexed #{Enum.count(objects)} entries")
56 end)
57 |> Stream.run()
58 end
59
60 def run(["clear"]) do
61 start_pleroma()
62
63 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
64
65 {:ok, _} = Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [], [])
66 end
67 end