2021-12-12 17:23:44 +00:00
|
|
|
defmodule Pleroma.Search.Elasticsearch do
|
|
|
|
@behaviour Pleroma.Search
|
|
|
|
|
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
2021-12-14 13:53:46 +00:00
|
|
|
alias Pleroma.Web.MastodonAPI.AccountView
|
2021-12-12 18:25:20 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
2021-12-14 13:53:46 +00:00
|
|
|
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-12 17:23:44 +00:00
|
|
|
%{
|
2021-12-14 14:33:34 +00:00
|
|
|
size: 50,
|
|
|
|
terminate_after: 50,
|
|
|
|
timeout: "5s",
|
2021-12-14 14:01:09 +00:00
|
|
|
sort: [
|
|
|
|
%{"_timestamp" => "desc"}
|
|
|
|
],
|
2021-12-14 13:53:46 +00:00
|
|
|
query: %{
|
|
|
|
bool: %{
|
|
|
|
must: Parsers.Activity.parse(query)
|
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-12 17:23:44 +00:00
|
|
|
%{
|
2021-12-14 14:01:09 +00:00
|
|
|
size: 50,
|
|
|
|
terminate_after: 50,
|
2021-12-14 14:33:34 +00:00
|
|
|
timeout: "5s",
|
2021-12-14 13:53:46 +00:00
|
|
|
query: %{
|
|
|
|
bool: %{
|
|
|
|
must: Parsers.User.parse(query)
|
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-12 17:23:44 +00:00
|
|
|
%{
|
2021-12-14 14:01:09 +00:00
|
|
|
size: 50,
|
|
|
|
terminate_after: 50,
|
2021-12-14 14:33:34 +00:00
|
|
|
timeout: "5s",
|
2021-12-14 13:53:46 +00:00
|
|
|
query: %{
|
|
|
|
bool: %{
|
|
|
|
must: Parsers.Hashtag.parse(query)
|
2021-12-12 17:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Pleroma.Search
|
|
|
|
def search(%{assigns: %{user: user}} = _conn, %{q: query} = _params, _options) do
|
2021-12-14 13:53:46 +00:00
|
|
|
parsed_query =
|
|
|
|
query
|
|
|
|
|> String.trim()
|
|
|
|
|> SearchParser.parse!()
|
2021-12-12 17:23:44 +00:00
|
|
|
|
2021-12-14 13:53:46 +00:00
|
|
|
activity_task =
|
|
|
|
Task.async(fn ->
|
|
|
|
q = es_query(:activity, parsed_query)
|
2021-12-12 17:23:44 +00:00
|
|
|
|
2021-12-14 13:53:46 +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)
|
2021-12-14 13:53:46 +00:00
|
|
|
end)
|
2021-12-12 17:23:44 +00:00
|
|
|
|
2021-12-14 13:53:46 +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)
|
|
|
|
|
|
|
|
%{
|
|
|
|
"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
|