1 defmodule Pleroma.Search.Meilisearch do
3 require Pleroma.Constants
7 import Pleroma.Search.DatabaseSearch
10 @behaviour Pleroma.Search.SearchBackend
13 private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
15 [{"Content-Type", "application/json"}] ++
16 if is_nil(private_key), do: [], else: [{"X-Meili-API-Key", private_key}]
19 def meili_get(path) do
20 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
24 Path.join(endpoint, path),
28 with {:ok, res} <- result do
29 {:ok, Jason.decode!(res.body)}
33 def meili_post(path, params) do
34 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
38 Path.join(endpoint, path),
39 Jason.encode!(params),
43 with {:ok, res} <- result do
44 {:ok, Jason.decode!(res.body)}
48 def meili_put(path, params) do
49 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
54 Path.join(endpoint, path),
55 Jason.encode!(params),
60 with {:ok, res} <- result do
61 {:ok, Jason.decode!(res.body)}
65 def meili_delete!(path) do
66 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
71 Path.join(endpoint, path),
78 def search(user, query, options \\ []) do
79 limit = Enum.min([Keyword.get(options, :limit), 40])
80 offset = Keyword.get(options, :offset, 0)
81 author = Keyword.get(options, :author)
85 "/indexes/objects/search",
86 %{q: query, offset: offset, limit: limit}
89 with {:ok, result} <- res do
90 hits = result["hits"] |> Enum.map(& &1["ap"])
94 |> Activity.create_by_object_ap_id()
95 |> Activity.with_preloaded_object()
96 |> Activity.with_preloaded_object()
97 |> Activity.restrict_deactivated_users()
98 |> maybe_restrict_local(user)
99 |> maybe_restrict_author(author)
100 |> maybe_restrict_blocked(user)
101 |> maybe_fetch(user, query)
102 |> order_by([object: obj], desc: obj.data["published"])
103 |> Pleroma.Repo.all()
105 _ -> maybe_fetch([], user, query)
110 def object_to_search_data(object) do
111 # Only index public or unlisted Notes
112 if not is_nil(object) and object.data["type"] == "Note" and
113 not is_nil(object.data["content"]) and
114 (Pleroma.Constants.as_public() in object.data["to"] or
115 Pleroma.Constants.as_public() in object.data["cc"]) and
116 String.length(object.data["content"]) > 1 do
120 case data["content"] do
121 [nil | rest] -> to_string(rest)
126 with {:ok, scrubbed} <- FastSanitize.strip_tags(content_str),
127 trimmed <- String.trim(scrubbed) do
131 if String.length(content) > 1 do
132 {:ok, published, _} = DateTime.from_iso8601(data["published"])
138 published: published |> DateTime.to_unix()
145 def add_to_index(activity) do
146 maybe_search_data = object_to_search_data(activity.object)
148 if activity.data["type"] == "Create" and maybe_search_data do
151 "/indexes/objects/documents",
155 with {:ok, res} <- result,
156 true <- Map.has_key?(res, "updateId") do
160 Logger.error("Failed to add activity #{activity.id} to index: #{inspect(result)}")
166 def remove_from_index(object) do
167 meili_delete!("/indexes/objects/documents/#{object.id}")