QdrantSearch: Implement post deletion

This commit is contained in:
Lain Soykaf 2024-05-18 14:04:32 +04:00
parent 933117785f
commit e3933a067f
2 changed files with 27 additions and 7 deletions

View file

@ -81,6 +81,19 @@ defmodule Pleroma.Search.QdrantSearch do
end end
end end
@impl true
def remove_from_index(object) do
activity = Activity.get_by_object_ap_id_with_object(object.data["id"])
id = activity.id |> FlakeId.from_string() |> Ecto.UUID.cast!()
with {:ok, %{status: 200}} <-
QdrantClient.post("/collections/posts/points/delete", %{"points" => [id]}) do
:ok
else
e -> {:error, e}
end
end
@impl true @impl true
def search(_user, query, _options) do def search(_user, query, _options) do
query = "Represent this sentence for searching relevant passages: #{query}" query = "Represent this sentence for searching relevant passages: #{query}"
@ -103,11 +116,6 @@ defmodule Pleroma.Search.QdrantSearch do
[] []
end end
end end
@impl true
def remove_from_index(_object) do
:ok
end
end end
defmodule Pleroma.Search.QdrantSearch.OllamaClient do defmodule Pleroma.Search.QdrantSearch.OllamaClient do

View file

@ -15,7 +15,7 @@ defmodule Pleroma.Search.QdrantSearchTest do
alias Pleroma.Workers.SearchIndexingWorker alias Pleroma.Workers.SearchIndexingWorker
describe "Qdrant search" do describe "Qdrant search" do
test "indexes a public post on creation" do test "indexes a public post on creation, deletes from the index on deletion" do
user = insert(:user) user = insert(:user)
Tesla.Mock.mock(fn Tesla.Mock.mock(fn
@ -29,10 +29,14 @@ defmodule Pleroma.Search.QdrantSearchTest do
assert match?(%{"points" => [%{"vector" => [1, 2, 3]}]}, Jason.decode!(body)) assert match?(%{"points" => [%{"vector" => [1, 2, 3]}]}, Jason.decode!(body))
Tesla.Mock.json("ok") Tesla.Mock.json("ok")
%{method: :post, url: "https://qdrant.url/collections/posts/points/delete"} ->
send(self(), "deleted_from_qdrant")
Tesla.Mock.json("ok")
end) end)
Config Config
|> expect(:get, 4, fn |> expect(:get, 6, fn
[Pleroma.Search, :module], nil -> [Pleroma.Search, :module], nil ->
QdrantSearch QdrantSearch
@ -60,6 +64,14 @@ defmodule Pleroma.Search.QdrantSearchTest do
assert :ok = perform_job(SearchIndexingWorker, args) assert :ok = perform_job(SearchIndexingWorker, args)
assert_received("posted_to_ollama") assert_received("posted_to_ollama")
assert_received("posted_to_qdrant") assert_received("posted_to_qdrant")
{:ok, _} = CommonAPI.delete(activity.id, user)
delete_args = %{"op" => "remove_from_index", "object" => activity.object.id}
assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
assert :ok = perform_job(SearchIndexingWorker, delete_args)
assert_received("deleted_from_qdrant")
end end
end end
end end