akkoma/lib/pleroma/elasticsearch/store.ex

116 lines
2.3 KiB
Elixir
Raw Normal View History

2021-12-11 10:36:49 -07:00
defmodule Pleroma.Elasticsearch do
alias Pleroma.Activity
alias Pleroma.Elasticsearch.DocumentMappings
2021-12-12 12:31:47 -07:00
alias Pleroma.Config
2021-12-11 10:36:49 -07:00
defp url do
2021-12-12 12:31:47 -07:00
Config.get([:elasticsearch, :url])
end
def put_by_id(id) do
id
|> Activity.get_by_id_with_object()
|> maybe_put_into_elasticsearch()
end
def maybe_put_into_elasticsearch({:ok, activity}) do
maybe_put_into_elasticsearch(activity)
end
2021-12-12 13:01:07 -07:00
def maybe_put_into_elasticsearch(%{data: %{"type" => "Create"}, object: %{data: %{"type" => "Note"}}} = activity) do
2021-12-12 12:31:47 -07:00
if Config.get([:search, :provider]) == Pleroma.Search.Elasticsearch do
actor = Pleroma.Activity.user_actor(activity)
activity
|> Map.put(:user_actor, actor)
|> put()
end
end
def maybe_put_into_elasticsearch(_) do
{:ok, :skipped}
2021-12-11 10:36:49 -07:00
end
def put(%Activity{} = activity) do
Elastix.Document.index(
2021-12-12 10:23:44 -07:00
url(),
"activities",
"activity",
DocumentMappings.Activity.id(activity),
DocumentMappings.Activity.encode(activity)
2021-12-11 10:36:49 -07:00
)
end
def bulk_post(data, :activities) do
2021-12-12 10:23:44 -07:00
d =
data
2021-12-13 13:27:35 -07:00
|> Enum.filter(fn x ->
t = x.object
|> Map.get(:data, %{})
|> Map.get("type", "")
t == "Note"
end)
2021-12-12 10:23:44 -07:00
|> Enum.map(fn d ->
2021-12-11 10:36:49 -07:00
[
2021-12-12 10:23:44 -07:00
%{index: %{_id: DocumentMappings.Activity.id(d)}},
DocumentMappings.Activity.encode(d)
2021-12-11 10:36:49 -07:00
]
2021-12-12 10:23:44 -07:00
end)
|> List.flatten()
2021-12-11 10:36:49 -07:00
2021-12-11 11:48:46 -07:00
Elastix.Bulk.post(
2021-12-12 10:23:44 -07:00
url(),
d,
index: "activities",
type: "activity"
2021-12-11 10:36:49 -07:00
)
end
2021-12-11 11:48:46 -07:00
2021-12-13 13:27:35 -07: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-14 05:37:10 -07:00
def bulk_post(data, :hashtags) do
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-12 10:23:44 -07:00
def search_activities(q) do
2021-12-11 11:48:46 -07:00
Elastix.Search.search(
2021-12-12 10:23:44 -07:00
url(),
"activities",
["activity"],
q
2021-12-11 11:48:46 -07:00
)
end
2021-12-11 10:36:49 -07:00
end