fix compatibility with meilisearch (#164)
[akkoma] / lib / pleroma / search / elasticsearch / store.ex
1 # Akkoma: A lightweight social networking server
2 # Copyright © 2022-2022 Akkoma Authors <https://git.ihatebeinga.live/IHBAGang/akkoma/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Search.Elasticsearch.Store do
6 @behaviour Elasticsearch.Store
7 alias Pleroma.Search.Elasticsearch.Cluster
8 require Logger
9
10 alias Pleroma.Repo
11
12 @impl true
13 def stream(schema) do
14 Repo.stream(schema)
15 end
16
17 @impl true
18 def transaction(fun) do
19 {:ok, result} = Repo.transaction(fun, timeout: :infinity)
20 result
21 end
22
23 def search(_, _, _, :skip), do: []
24
25 def search(:raw, index, q) do
26 with {:ok, raw_results} <- Elasticsearch.post(Cluster, "/#{index}/_search", q) do
27 results =
28 raw_results
29 |> Map.get("hits", %{})
30 |> Map.get("hits", [])
31
32 {:ok, results}
33 else
34 {:error, e} ->
35 Logger.error(e)
36 {:error, e}
37 end
38 end
39
40 def search(:activities, q) do
41 with {:ok, results} <- search(:raw, "activities", q) do
42 results
43 |> Enum.map(fn result -> result["_id"] end)
44 |> Pleroma.Activity.all_by_ids_with_object()
45 |> Enum.sort(&(&1.inserted_at >= &2.inserted_at))
46 else
47 e ->
48 Logger.error(e)
49 []
50 end
51 end
52 end