akkoma/lib/pleroma/elasticsearch/store.ex

47 lines
888 B
Elixir
Raw Normal View History

2021-12-11 17:36:49 +00:00
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(
2021-12-12 17:23:44 +00:00
url(),
"activities",
"activity",
DocumentMappings.Activity.id(activity),
DocumentMappings.Activity.encode(activity)
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
|> 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-11 18:48:46 +00:00
Elastix.Bulk.post(
2021-12-12 17:23:44 +00:00
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-12 17:23:44 +00:00
def search_activities(q) do
2021-12-11 18:48:46 +00:00
Elastix.Search.search(
2021-12-12 17:23:44 +00:00
url(),
"activities",
["activity"],
q
2021-12-11 18:48:46 +00:00
)
end
2021-12-11 17:36:49 +00:00
end