akkoma/lib/pleroma/search/elasticsearch.ex

158 lines
3.4 KiB
Elixir
Raw Normal View History

2021-12-12 17:23:44 +00:00
defmodule Pleroma.Search.Elasticsearch do
@behaviour Pleroma.Search
2021-12-15 10:57:47 +00:00
alias Pleroma.Activity
alias Pleroma.Object.Fetcher
2021-12-12 17:23:44 +00:00
alias Pleroma.Web.MastodonAPI.StatusView
alias Pleroma.Web.MastodonAPI.AccountView
2021-12-12 18:25:20 +00:00
alias Pleroma.Web.ActivityPub.Visibility
alias Pleroma.Search.Elasticsearch.Parsers
alias Pleroma.Web.Endpoint
2021-12-12 17:23:44 +00:00
2021-12-14 14:48:24 +00:00
def es_query(:activity, query) do
2021-12-14 15:02:11 +00:00
must = Parsers.Activity.parse(query)
if must == [] do
:skip
else
%{
size: 50,
terminate_after: 50,
timeout: "5s",
sort: [
2021-12-15 10:57:47 +00:00
"_score",
%{_timestamp: %{order: "desc", format: "basic_date_time"}}
2021-12-14 15:02:11 +00:00
],
query: %{
bool: %{
must: must
}
2021-12-12 17:23:44 +00:00
}
}
2021-12-14 15:02:11 +00:00
end
2021-12-12 17:23:44 +00:00
end
2021-12-14 14:48:24 +00:00
def es_query(:user, query) do
2021-12-14 15:02:11 +00:00
must = Parsers.User.parse(query)
if must == [] do
:skip
else
%{
size: 50,
terminate_after: 50,
timeout: "5s",
2021-12-15 10:57:47 +00:00
sort: [
"_score"
],
2021-12-14 15:02:11 +00:00
query: %{
bool: %{
must: must
}
2021-12-12 17:23:44 +00:00
}
}
2021-12-14 15:02:11 +00:00
end
2021-12-12 17:23:44 +00:00
end
2021-12-14 14:48:24 +00:00
def es_query(:hashtag, query) do
2021-12-14 15:02:11 +00:00
must = Parsers.Hashtag.parse(query)
if must == [] do
:skip
else
%{
size: 50,
terminate_after: 50,
timeout: "5s",
2021-12-15 10:57:47 +00:00
sort: [
"_score"
],
2021-12-14 15:02:11 +00:00
query: %{
bool: %{
must: Parsers.Hashtag.parse(query)
}
2021-12-12 17:23:44 +00:00
}
}
2021-12-14 15:02:11 +00:00
end
2021-12-12 17:23:44 +00:00
end
2021-12-15 10:57:47 +00:00
defp maybe_fetch(:activity, search_query) do
with true <- Regex.match?(~r/https?:/, search_query),
{:ok, object} <- Fetcher.fetch_object_from_id(search_query),
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]) do
activity
else
_ -> nil
end
end
2021-12-12 17:23:44 +00:00
@impl Pleroma.Search
def search(%{assigns: %{user: user}} = _conn, %{q: query} = _params, _options) do
parsed_query =
query
|> String.trim()
|> SearchParser.parse!()
2021-12-12 17:23:44 +00:00
2021-12-15 10:57:47 +00:00
activity_fetch_task =
Task.async(fn ->
maybe_fetch(:activity, String.trim(query))
end)
activity_task =
Task.async(fn ->
q = es_query(:activity, parsed_query)
2021-12-12 17:23:44 +00:00
Pleroma.Elasticsearch.search(:activities, q)
2021-12-14 13:58:16 +00:00
|> Enum.filter(fn x -> Visibility.visible_for_user?(x, user) end)
end)
2021-12-12 17:23:44 +00:00
user_task =
Task.async(fn ->
q = es_query(:user, parsed_query)
Pleroma.Elasticsearch.search(:users, q)
|> Enum.filter(fn x -> Pleroma.User.visible_for(x, user) == :visible end)
end)
hashtag_task =
Task.async(fn ->
q = es_query(:hashtag, parsed_query)
Pleroma.Elasticsearch.search(:hashtags, q)
end)
activity_results = Task.await(activity_task)
user_results = Task.await(user_task)
hashtag_results = Task.await(hashtag_task)
2021-12-15 10:57:47 +00:00
direct_activity = Task.await(activity_fetch_task)
activity_results =
if direct_activity == nil do
activity_results
else
[direct_activity | activity_results]
end
%{
"accounts" =>
AccountView.render("index.json",
users: user_results,
for: user
),
"hashtags" =>
Enum.map(hashtag_results, fn x ->
%{
url: Endpoint.url() <> "/tag/" <> x,
name: x
}
end),
"statuses" =>
StatusView.render("index.json",
activities: activity_results,
for: user,
as: :activity
)
}
2021-12-12 17:23:44 +00:00
end
end