2021-12-11 17:36:49 +00:00
|
|
|
defmodule Pleroma.Elasticsearch do
|
|
|
|
alias Pleroma.Activity
|
2021-12-14 13:53:46 +00:00
|
|
|
alias Pleroma.User
|
2021-12-16 15:58:56 +00:00
|
|
|
alias Pleroma.Object
|
2021-12-11 17:36:49 +00:00
|
|
|
alias Pleroma.Elasticsearch.DocumentMappings
|
2021-12-12 19:31:47 +00:00
|
|
|
alias Pleroma.Config
|
2021-12-14 13:53:46 +00:00
|
|
|
require Logger
|
2021-12-11 17:36:49 +00:00
|
|
|
|
|
|
|
defp url do
|
2021-12-12 19:31:47 +00:00
|
|
|
Config.get([:elasticsearch, :url])
|
|
|
|
end
|
|
|
|
|
2021-12-14 13:53:46 +00:00
|
|
|
defp enabled? do
|
|
|
|
Config.get([:search, :provider]) == Pleroma.Search.Elasticsearch
|
|
|
|
end
|
|
|
|
|
|
|
|
def put_by_id(:activity, id) do
|
2021-12-12 19:31:47 +00:00
|
|
|
id
|
|
|
|
|> Activity.get_by_id_with_object()
|
|
|
|
|> maybe_put_into_elasticsearch()
|
|
|
|
end
|
|
|
|
|
2021-12-14 13:53:46 +00:00
|
|
|
def maybe_put_into_elasticsearch({:ok, item}) do
|
|
|
|
maybe_put_into_elasticsearch(item)
|
2021-12-12 19:31:47 +00:00
|
|
|
end
|
|
|
|
|
2021-12-14 13:53:46 +00:00
|
|
|
def maybe_put_into_elasticsearch(
|
|
|
|
%{data: %{"type" => "Create"}, object: %{data: %{"type" => "Note"}}} = activity
|
|
|
|
) do
|
|
|
|
if enabled?() do
|
2021-12-12 19:31:47 +00:00
|
|
|
actor = Pleroma.Activity.user_actor(activity)
|
|
|
|
|
|
|
|
activity
|
|
|
|
|> Map.put(:user_actor, actor)
|
|
|
|
|> put()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-15 11:05:30 +00:00
|
|
|
def maybe_put_into_elasticsearch(%User{actor_type: "Person"} = user) do
|
2021-12-14 13:53:46 +00:00
|
|
|
if enabled?() do
|
|
|
|
put(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-12 19:31:47 +00:00
|
|
|
def maybe_put_into_elasticsearch(_) do
|
|
|
|
{:ok, :skipped}
|
2021-12-11 17:36:49 +00:00
|
|
|
end
|
|
|
|
|
2021-12-16 15:58:56 +00:00
|
|
|
def maybe_bulk_post(data, type) do
|
|
|
|
if enabled?() do
|
|
|
|
bulk_post(data, type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-11 17:36:49 +00:00
|
|
|
def put(%Activity{} = activity) do
|
2021-12-14 14:28:15 +00:00
|
|
|
{:ok, _} =
|
|
|
|
Elastix.Document.index(
|
|
|
|
url(),
|
|
|
|
"activities",
|
|
|
|
"activity",
|
|
|
|
DocumentMappings.Activity.id(activity),
|
|
|
|
DocumentMappings.Activity.encode(activity)
|
|
|
|
)
|
2021-12-16 15:58:56 +00:00
|
|
|
|
|
|
|
activity
|
|
|
|
|> Map.get(:object)
|
|
|
|
|> Object.hashtags()
|
|
|
|
|> Enum.map(fn x -> %{id: x, name: x, timestamp: DateTime.to_iso8601(DateTime.utc_now())} end)
|
|
|
|
|> bulk_post(:hashtags)
|
2021-12-14 13:53:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def put(%User{} = user) do
|
2021-12-14 14:28:15 +00:00
|
|
|
{:ok, _} =
|
|
|
|
Elastix.Document.index(
|
|
|
|
url(),
|
|
|
|
"users",
|
|
|
|
"user",
|
|
|
|
DocumentMappings.User.id(user),
|
|
|
|
DocumentMappings.User.encode(user)
|
|
|
|
)
|
2021-12-11 17:36:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_post(data, :activities) do
|
2021-12-12 17:23:44 +00:00
|
|
|
d =
|
|
|
|
data
|
2021-12-13 20:27:35 +00:00
|
|
|
|> Enum.filter(fn x ->
|
2021-12-14 13:53:46 +00:00
|
|
|
t =
|
|
|
|
x.object
|
|
|
|
|> Map.get(:data, %{})
|
|
|
|
|> Map.get("type", "")
|
|
|
|
|
2021-12-13 20:27:35 +00:00
|
|
|
t == "Note"
|
|
|
|
end)
|
2021-12-12 17:23:44 +00:00
|
|
|
|> Enum.map(fn d ->
|
2021-12-11 17:36:49 +00:00
|
|
|
[
|
2021-12-12 17:23:44 +00:00
|
|
|
%{index: %{_id: DocumentMappings.Activity.id(d)}},
|
|
|
|
DocumentMappings.Activity.encode(d)
|
2021-12-11 17:36:49 +00:00
|
|
|
]
|
2021-12-12 17:23:44 +00:00
|
|
|
end)
|
|
|
|
|> List.flatten()
|
2021-12-11 17:36:49 +00:00
|
|
|
|
2021-12-14 14:28:15 +00:00
|
|
|
{:ok, %{body: %{"errors" => false}}} =
|
|
|
|
Elastix.Bulk.post(
|
|
|
|
url(),
|
|
|
|
d,
|
|
|
|
index: "activities",
|
|
|
|
type: "activity"
|
|
|
|
)
|
2021-12-11 17:36:49 +00:00
|
|
|
end
|
2021-12-11 18:48:46 +00:00
|
|
|
|
2021-12-13 20:27:35 +00:00
|
|
|
def bulk_post(data, :users) do
|
|
|
|
d =
|
|
|
|
data
|
|
|
|
|> Enum.map(fn d ->
|
|
|
|
[
|
|
|
|
%{index: %{_id: DocumentMappings.User.id(d)}},
|
|
|
|
DocumentMappings.User.encode(d)
|
|
|
|
]
|
|
|
|
end)
|
|
|
|
|> List.flatten()
|
|
|
|
|
|
|
|
Elastix.Bulk.post(
|
|
|
|
url(),
|
|
|
|
d,
|
|
|
|
index: "users",
|
|
|
|
type: "user"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-12-16 15:58:56 +00:00
|
|
|
def bulk_post(data, :hashtags) when is_list(data) do
|
2021-12-14 12:37:10 +00:00
|
|
|
d =
|
|
|
|
data
|
|
|
|
|> Enum.map(fn d ->
|
|
|
|
[
|
|
|
|
%{index: %{_id: DocumentMappings.Hashtag.id(d)}},
|
|
|
|
DocumentMappings.Hashtag.encode(d)
|
|
|
|
]
|
|
|
|
end)
|
|
|
|
|> List.flatten()
|
|
|
|
|
|
|
|
Elastix.Bulk.post(
|
|
|
|
url(),
|
|
|
|
d,
|
|
|
|
index: "hashtags",
|
|
|
|
type: "hashtag"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-12-14 14:48:24 +00:00
|
|
|
def bulk_post(_, :hashtags), do: {:ok, nil}
|
|
|
|
|
2021-12-14 15:02:11 +00:00
|
|
|
def search(_, _, _, :skip), do: []
|
|
|
|
|
2021-12-14 13:53:46 +00:00
|
|
|
def search(:raw, index, type, q) do
|
|
|
|
with {:ok, raw_results} <- Elastix.Search.search(url(), index, [type], q) do
|
|
|
|
results =
|
|
|
|
raw_results
|
|
|
|
|> Map.get(:body, %{})
|
|
|
|
|> Map.get("hits", %{})
|
|
|
|
|> Map.get("hits", [])
|
|
|
|
|
|
|
|
{:ok, results}
|
|
|
|
else
|
|
|
|
{:error, e} ->
|
|
|
|
Logger.error(e)
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(:activities, q) do
|
|
|
|
with {:ok, results} <- search(:raw, "activities", "activity", q) do
|
|
|
|
results
|
|
|
|
|> Enum.map(fn result -> result["_id"] end)
|
|
|
|
|> Pleroma.Activity.all_by_ids_with_object()
|
2021-12-15 10:57:47 +00:00
|
|
|
|> Enum.sort(&(&1.inserted_at >= &2.inserted_at))
|
2021-12-14 13:53:46 +00:00
|
|
|
else
|
|
|
|
e ->
|
|
|
|
Logger.error(e)
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(:users, q) do
|
|
|
|
with {:ok, results} <- search(:raw, "users", "user", q) do
|
|
|
|
results
|
|
|
|
|> Enum.map(fn result -> result["_id"] end)
|
|
|
|
|> Pleroma.User.get_all_by_ids()
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
Logger.error(e)
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(:hashtags, q) do
|
|
|
|
with {:ok, results} <- search(:raw, "hashtags", "hashtag", q) do
|
|
|
|
results
|
|
|
|
|> Enum.map(fn result -> result["_source"]["hashtag"] end)
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
Logger.error(e)
|
|
|
|
[]
|
|
|
|
end
|
2021-12-11 18:48:46 +00:00
|
|
|
end
|
2021-12-11 17:36:49 +00:00
|
|
|
end
|