This commit is contained in:
Mark Felder 2024-02-15 12:06:56 -05:00
parent 03834454d9
commit 122158a6ab
4 changed files with 27 additions and 27 deletions

View file

@ -1,17 +1,7 @@
defmodule Pleroma.Search do
alias Pleroma.Workers.SearchIndexingWorker
alias Pleroma.Config
def add_to_index(%Pleroma.Activity{id: activity_id}) do
SearchIndexingWorker.enqueue("add_to_index", %{"activity" => activity_id})
end
def remove_from_index(%Pleroma.Object{id: object_id}) do
SearchIndexingWorker.enqueue("remove_from_index", %{"object" => object_id})
end
def search(query, options) do
search_module = Pleroma.Config.get([Pleroma.Search, :module], Pleroma.Activity)
search_module.search(options[:for_user], query, options)
end
defdelegate add_to_index(activity), to: Config.get([Pleroma.Search, :module])
defdelegate remove_from_index(object), to: Config.get([Pleroma.Search, :module])
defdelegate search(query, options), to: Config.get([Pleroma.Search, :module])
end

View file

@ -4,6 +4,7 @@ defmodule Pleroma.Search.Meilisearch do
alias Pleroma.Activity
alias Pleroma.Config.Getting, as: Config
alias Pleroma.Workers.SearchIndexingWorker
import Pleroma.Search.DatabaseSearch
import Ecto.Query
@ -149,7 +150,13 @@ defmodule Pleroma.Search.Meilisearch do
end
@impl true
def add_to_index(activity) do
def add_to_index(%Pleroma.Activity{id: activity_id}) do
SearchIndexingWorker.enqueue("add_to_index", %{"activity" => activity_id})
end
def add_to_index(activity_id) when is_binary(activity_id) do
activity = Pleroma.Activity.get_by_id_with_object(activity_id)
maybe_search_data = object_to_search_data(activity.object)
if activity.data["type"] == "Create" and maybe_search_data do
@ -175,7 +182,11 @@ defmodule Pleroma.Search.Meilisearch do
end
@impl true
def remove_from_index(object) do
meili_delete("/indexes/objects/documents/#{object.id}")
def remove_from_index(%Pleroma.Object{id: object_id}) do
SearchIndexingWorker.enqueue("remove_from_index", %{"object" => object_id})
end
def remove_from_index(object_id) when is_binary(object_id) do
meili_delete("/indexes/objects/documents/#{object_id}")
end
end

View file

@ -8,10 +8,10 @@ defmodule Pleroma.Search.SearchBackend do
@doc """
Add the object associated with the activity to the search index.
The whole activity is passed, to allow filtering on things such as scope.
When the activity is passed we schedule the job; when the activity id is passed we execute the work.
"""
@callback add_to_index(activity :: Pleroma.Activity.t()) :: :ok | {:error, any()}
@callback add_to_index(activity :: Pleroma.Activity.t()) :: {:ok, Oban.Job.t()} | {:error, Oban.Job.changeset() | term()}
@callback add_to_index(activity_id :: String.t()) :: :ok | {:error, any()}
@doc """
Remove the object from the index.
@ -19,6 +19,9 @@ defmodule Pleroma.Search.SearchBackend do
Just the object, as opposed to the whole activity, is passed, since the object
is what contains the actual content and there is no need for filtering when removing
from index.
When the object is passed we schedule the job; when the object id is passed we execute the work.
"""
@callback remove_from_index(object :: Pleroma.Object.t()) :: :ok | {:error, any()}
@callback remove_from_index(object :: Pleroma.Object.t()) :: {:ok, Oban.Job.t()} | {:error, Oban.Job.changeset() | term()}
@callback remove_from_index(object_id :: String.t()) :: :ok | {:error, any()}
end

View file

@ -6,18 +6,14 @@ defmodule Pleroma.Workers.SearchIndexingWorker do
alias Pleroma.Config.Getting, as: Config
def perform(%Job{args: %{"op" => "add_to_index", "activity" => activity_id}}) do
activity = Pleroma.Activity.get_by_id_with_object(activity_id)
search_module = Config.get([Pleroma.Search, :module])
search_module.add_to_index(activity)
search_module.add_to_index(activity_id)
end
def perform(%Job{args: %{"op" => "remove_from_index", "object" => object_id}}) do
object = Pleroma.Object.get_by_id(object_id)
search_module = Config.get([Pleroma.Search, :module])
search_module.remove_from_index(object)
search_module.remove_from_index(object_id)
end
end