parent
fb0aa0661c
commit
c684265585
@ -0,0 +1,46 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Mix.Tasks.Pleroma.Search do
|
||||
use Mix.Task
|
||||
import Mix.Pleroma
|
||||
import Ecto.Query
|
||||
alias Pleroma.Elasticsearch
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Pagination
|
||||
|
||||
@shortdoc "Manages elasticsearch"
|
||||
|
||||
def run(["import" | rest]) do
|
||||
start_pleroma()
|
||||
|
||||
query = from(a in Activity, where: not ilike(a.actor, "%/relay"))
|
||||
|> Activity.with_preloaded_object
|
||||
|> Activity.with_preloaded_user_actor
|
||||
|> get_all
|
||||
end
|
||||
|
||||
defp get_all(query, max_id \\ nil) do
|
||||
params = %{limit: 20}
|
||||
params = if max_id == nil do
|
||||
params
|
||||
else
|
||||
Map.put(params, :max_id, max_id)
|
||||
end
|
||||
|
||||
res = query
|
||||
|> Pagination.fetch_paginated(params)
|
||||
|
||||
if res == [] do
|
||||
:ok
|
||||
else
|
||||
res
|
||||
|> Pleroma.Elasticsearch.bulk_post(:activities)
|
||||
|
||||
get_all(query, List.last(res).id)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,13 @@
|
||||
defmodule Pleroma.Elasticsearch.DocumentMappings.Activity do
|
||||
alias Pleroma.Object
|
||||
|
||||
def id(obj), do: obj.id
|
||||
def encode(%{object: %{data: %{ "type" => "Note" }}} = activity) do
|
||||
%{
|
||||
user: activity.user_actor.nickname,
|
||||
content: activity.object.data["content"],
|
||||
instance: URI.parse(activity.user_actor.ap_id).host,
|
||||
hashtags: Object.hashtags(activity.object)
|
||||
}
|
||||
end
|
||||
end
|
@ -0,0 +1,36 @@
|
||||
defmodule Pleroma.Elasticsearch do
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Elasticsearch.DocumentMappings
|
||||
|
||||
defp url do
|
||||
Pleroma.Config.get([:elasticsearch, :url])
|
||||
end
|
||||
|
||||
def put(%Activity{} = activity) do
|
||||
Elastix.Document.index(
|
||||
url(),
|
||||
"activities",
|
||||
"activity",
|
||||
DocumentMappings.Activity.id(activity),
|
||||
DocumentMappings.Activity.encode(activity)
|
||||
)
|
||||
end
|
||||
|
||||
def bulk_post(data, :activities) do
|
||||
d = data
|
||||
|> Enum.map(fn d ->
|
||||
[
|
||||
%{index: %{_id: DocumentMappings.Activity.id(d)}},
|
||||
DocumentMappings.Activity.encode(d)
|
||||
]
|
||||
end)
|
||||
|> List.flatten()
|
||||
|
||||
IO.inspect Elastix.Bulk.post(
|
||||
url(),
|
||||
d,
|
||||
index: "activities",
|
||||
type: "activity"
|
||||
)
|
||||
end
|
||||
end
|
Loading…
Reference in new issue