import Mix.Pleroma
import Ecto.Query
+ import Pleroma.Search.Meilisearch, only: [meili_post!: 2, meili_delete!: 1]
+
def run(["index"]) do
start_pleroma()
- endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
-
- {:ok, _} =
- Pleroma.HTTP.post(
- "#{endpoint}/indexes/objects/settings/ranking-rules",
- Jason.encode!([
- "desc(published)",
- "typo",
- "words",
- "proximity",
- "attribute",
- "wordsPosition",
- "exactness"
- ])
- )
+ meili_post!(
+ "/indexes/objects/settings/ranking-rules",
+ [
+ "desc(published)",
+ "typo",
+ "words",
+ "proximity",
+ "attribute",
+ "wordsPosition",
+ "exactness"
+ ]
+ )
- {:ok, _} =
- Pleroma.HTTP.post(
- "#{endpoint}/indexes/objects/settings/searchable-attributes",
- Jason.encode!([
- "content"
- ])
- )
+ meili_post!(
+ "/indexes/objects/settings/searchable-attributes",
+ [
+ "content"
+ ]
+ )
chunk_size = 10_000
{[objects], new_acc}
end)
|> Stream.each(fn objects ->
- {:ok, result} =
- Pleroma.HTTP.post(
- "#{endpoint}/indexes/objects/documents",
- Jason.encode!(objects)
+ result =
+ meili_post!(
+ "/indexes/objects/documents",
+ objects
)
- if not Map.has_key?(Jason.decode!(result.body), "updateId") do
- IO.puts("Failed to index: #{result}")
+ if not Map.has_key?(result, "updateId") do
+ IO.puts("Failed to index: #{inspect(result)}")
end
end)
|> Stream.run()
def run(["clear"]) do
start_pleroma()
+ meili_delete!("/indexes/objects/documents")
+ end
+
+ def run(["show-private-key", master_key]) do
+ start_pleroma()
+
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
- {:ok, _} =
- Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [],
- timeout: :infinity
+ {:ok, result} =
+ Pleroma.HTTP.get(
+ Path.join(endpoint, "/keys"),
+ [{"X-Meili-API-Key", master_key}]
)
+
+ decoded = Jason.decode!(result.body)
+
+ if decoded["private"] do
+ IO.puts(decoded["private"])
+ else
+ IO.puts("Error fetching the key, check the master key is correct: #{inspect(decoded)}")
+ end
end
end
import Pleroma.Activity.Search
import Ecto.Query
- def search(user, query, options \\ []) do
- limit = Enum.min([Keyword.get(options, :limit), 40])
- offset = Keyword.get(options, :offset, 0)
- author = Keyword.get(options, :author)
+ defp meili_headers() do
+ private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
+ if is_nil(private_key), do: [], else: [{"X-Meili-API-Key", private_key}]
+ end
+
+ def meili_post!(path, params) do
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
{:ok, result} =
Pleroma.HTTP.post(
- "#{endpoint}/indexes/objects/search",
- Jason.encode!(%{q: query, offset: offset, limit: limit})
+ Path.join(endpoint, path),
+ Jason.encode!(params),
+ meili_headers()
+ )
+
+ Jason.decode!(result.body)
+ end
+
+ def meili_delete!(path) do
+ endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
+
+ {:ok, _} =
+ Pleroma.HTTP.request(
+ :delete,
+ Path.join(endpoint, path),
+ "",
+ meili_headers(),
+ timeout: :infinity
+ )
+ end
+
+ def search(user, query, options \\ []) do
+ limit = Enum.min([Keyword.get(options, :limit), 40])
+ offset = Keyword.get(options, :offset, 0)
+ author = Keyword.get(options, :author)
+
+ result =
+ meili_post!(
+ "/indexes/objects/search",
+ %{q: query, offset: offset, limit: limit}
)
- hits = Jason.decode!(result.body)["hits"] |> Enum.map(& &1["ap"])
+ hits = result["hits"] |> Enum.map(& &1["ap"])
try do
hits
maybe_search_data = object_to_search_data(activity)
if activity.data["type"] == "Create" and maybe_search_data do
- endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
-
- {:ok, result} =
- Pleroma.HTTP.post(
- "#{endpoint}/indexes/objects/documents",
- Jason.encode!([maybe_search_data])
+ result =
+ meili_post!(
+ "/indexes/objects/documents",
+ [maybe_search_data]
)
- if not Map.has_key?(Jason.decode!(result.body), "updateId") do
- Logger.error("Failed to add activity #{activity.id} to index: #{result.body}")
+ if not Map.has_key?(result, "updateId") do
+ Logger.error("Failed to add activity #{activity.id} to index: #{inspect(result)}")
end
end
end
def remove_from_index(object) do
- endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
-
- {:ok, _} =
- Pleroma.HTTP.request(
- :delete,
- "#{endpoint}/indexes/objects/documents/#{object.id}",
- "",
- [],
- []
- )
+ meili_delete!("/indexes/objects/documents/#{object.id}")
end
end