make search provider configurable
[akkoma] / lib / pleroma / search / elasticsearch.ex
1 defmodule Pleroma.Search.Elasticsearch do
2 @behaviour Pleroma.Search
3
4 alias Pleroma.Web.MastodonAPI.StatusView
5
6 defp to_es(term) when is_binary(term) do
7 %{
8 match: %{
9 content: %{
10 query: term,
11 operator: "AND"
12 }
13 }
14 }
15 end
16
17 defp to_es({:quoted, term}), do: to_es(term)
18
19 defp to_es({:filter, ["hashtag", query]}) do
20 %{
21 term: %{
22 hashtags: %{
23 value: query
24 }
25 }
26 }
27 end
28
29 defp to_es({:filter, [field, query]}) do
30 %{
31 term: %{
32 field => %{
33 value: query
34 }
35 }
36 }
37 end
38
39 defp parse(query) do
40 query
41 |> SearchParser.parse!()
42 |> Enum.map(&to_es/1)
43 end
44
45 @impl Pleroma.Search
46 def search(%{assigns: %{user: user}} = _conn, %{q: query} = _params, _options) do
47 q = %{
48 query: %{
49 bool: %{
50 must: parse(query)
51 }
52 }
53 }
54
55 IO.inspect(q)
56
57 out = Pleroma.Elasticsearch.search_activities(q)
58
59 with {:ok, raw_results} <- out do
60 results =
61 raw_results
62 |> Map.get(:body, %{})
63 |> Map.get("hits", %{})
64 |> Map.get("hits", [])
65 |> Enum.map(fn result -> result["_id"] end)
66 |> Pleroma.Activity.all_by_ids_with_object()
67
68 %{
69 "accounts" => [],
70 "hashtags" => [],
71 "statuses" =>
72 StatusView.render("index.json",
73 activities: results,
74 for: user,
75 as: :activity
76 )
77 }
78 end
79 end
80 end