Add logging to milisiearch index and make it use desc(id)
[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
8 import Mix.Pleroma
9 import Ecto.Query
10
11 def run(["index"]) do
12 start_pleroma()
13
14 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
15
16 {:ok, _} =
17 Pleroma.HTTP.post(
18 "#{endpoint}/indexes/objects/settings/ranking-rules",
19 Jason.encode!([
20 "desc(id)",
21 "typo",
22 "words",
23 "proximity",
24 "attribute",
25 "wordsPosition",
26 "exactness"
27 ])
28 )
29
30 Pleroma.Repo.chunk_stream(
31 from(Pleroma.Object,
32 where: fragment("data->>'type' = 'Note'") and fragment("LENGTH(data->>'source') > 0")
33 ),
34 200,
35 :batches
36 )
37 |> Stream.map(fn objects ->
38 Enum.map(objects, fn object ->
39 data = object.data
40 %{id: object.id, source: data["source"], ap: data["id"]}
41 end)
42 end)
43 |> Stream.each(fn objects ->
44 {:ok, _} =
45 Pleroma.HTTP.post(
46 "#{endpoint}/indexes/objects/documents",
47 Jason.encode!(objects)
48 )
49
50 IO.puts("Indexed #{Enum.count(objects)} entries")
51 end)
52 |> Stream.run()
53 end
54 end