Make meilisearch sort on publish date converted to unix time
[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(published)",
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
58 {:ok, published, _} = DateTime.from_iso8601(data["published"])
59
60 %{
61 id: object.id,
62 source: data["source"],
63 ap: data["id"],
64 published: published |> DateTime.to_unix()
65 }
66 end)
67 end)
68 |> Stream.each(fn objects ->
69 {:ok, _} =
70 Pleroma.HTTP.post(
71 "#{endpoint}/indexes/objects/documents",
72 Jason.encode!(objects)
73 )
74 end)
75 |> Stream.run()
76 end,
77 timeout: :infinity
78 )
79 end
80
81 def run(["clear"]) do
82 start_pleroma()
83
84 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
85
86 {:ok, _} = Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [], [])
87 end
88 end